Culture info is not get query string when I change a language from English to German.
Startup.cs
services.Configure<RequestLocalizationOptions>(options =>
{
var supportedCultures = new[]
{
new CultureInfo("de-DE"),
new CultureInfo("en-US"),
};
options.DefaultRequestCulture = new RequestCulture(culture: "de-DE", uiCulture: "de-DE");
options.SupportedCultures = supportedCultures;
options.SupportedUICultures = supportedCultures;
options.RequestCultureProviders = new List<IRequestCultureProvider>
{
new QueryStringRequestCultureProvider(),
new CookieRequestCultureProvider()
};
});
It works properly when there is no query string in url. But I want to return that particular url with full query string. I wrote a method to set culture like this:
[HttpPost]
public IActionResult SetLanguage(string culture, string returnUrl)
{
Response.Cookies.Append(
CookieRequestCultureProvider.DefaultCookieName,
CookieRequestCultureProvider.MakeCookieValue(new RequestCulture(culture)),
new CookieOptions { Expires = DateTimeOffset.UtcNow.AddYears(1) }
);
return LocalRedirect(returnUrl);
}
_Layout.cshtml
<form id="selectLanguage" asp-controller="Home"
asp-action="SetLanguage" asp-route-returnUrl="#returnUrl"
method="post" class="form-horizontal" role="form">
<select name="culture" onchange="this.form.submit();"
asp-for="#requestCulture.RequestCulture.UICulture.Name"
asp-items="cultureItems">
</select>
</form>
When I'm changing the lang, then it creates a url as shown here:
How can I get full query string like this:
Try change your returnUrl like below :
var returnUrl = string.IsNullOrEmpty(Context.Request.Path) ? "~/" : $"~{Context.Request.Path.Value+Context.Request.QueryString.Value}";
I would suggest following solution since the accepted does not work when you're url contains a pathbase (which is the case for example when you host your service in IIS in with a virtual path):
returnUrl = UriHelper.BuildRelative(Context.Request.PathBase, Context.Request.Path, Context.Request.QueryString)
Related
I am following a document on how to recieve data from a database-site where I have my own API-key that is needed to get it out. However I have trouble reaching the JSONdata.
These are the documents I follow.
https://cbis-rest-api.citybreak.com/v1/swagger/ui/index
https://visit.github.io/api-doc/
This is what my code currently looks like:
static public class myData
{
static string apiKey = "myApiKeyNumber";
static string apiApp = "application/json";
static public async Task<JObject> testing()
{
var httpClientRequest = new HttpClient();
httpClientRequest.DefaultRequestHeaders.Add("ApiKey", apiKey);
httpClientRequest.DefaultRequestHeaders.Add("Accept", apiApp);
var result = await httpClientRequest.GetAsync("https://cbis-rest-api.citybreak.com/v1/api/raw/product/");
var resultString = await result.Content.ReadAsStringAsync();
System.Diagnostics.Debug.WriteLine(resultString);
var jsonResult = JObject.Parse(resultString);
System.Diagnostics.Debug.WriteLine(jsonResult);
}
}
And when I want to work with it:
async void loadData()
{
var getInfo = await phpApi.testing();
if (getInfo == null)
{
System.Diagnostics.Debug.WriteLine("no data");
}
else {
System.Diagnostics.Debug.WriteLine("data");
}
}
I think that the httpadress i entered in my JObject task must be wrong because when I run this code I get the crash: "Unexpected character encountered while parsing value: <. Path ", line 0, position 0.".
And when i run the "resultString" System.Diagnostics.Debug.WriteLine(resultString); in the log i get this:
<!DOCTYPE html PUBLIC "-/W3C/DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml/DTD/xhtm1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"/>
<title>404 - File or directory not found.</title>
<style type="text/css">`
Which means that the http (https://cbis-rest-api.citybreak.com/v1/api/raw/product/) does not have the json.
When I look at the documents i think that i am following the correct instruction however so i am a bit confused.
Any help, guidance would be very appreciated. I am not used to working with databases so i appreciate the help a lot!
UPDATED:
static public class myData
{
static string apiKey = "myApiKeyNumber";
static string apiApp = "application/json";
static public async Task<JObject> testing()
{
var httpClientRequest = new HttpClient();
httpClientRequest.DefaultRequestHeaders.Add("ApiKey", apiKey);
httpClientRequest.DefaultRequestHeaders.Add("Accept", apiApp);
var result = await httpClientRequest.GetAsync("https://cbis-rest-api.citybreak.com/v1/api/raw/product/getpaged/1");
var resultString = await result.Content.ReadAsStringAsync();
System.Diagnostics.Debug.WriteLine(resultString);
var jsonResult = JObject.Parse(resultString);
return jsonResult;
}
}
With this code I get the error "Error reading JObject from JsonReader, Path ", line 0, position 0", and i get nothing in the log from the resultString which means that there is no JSON there? I try with many different ID's but with same result. I do not quite know where i find the id of the data either.
The documentation for the API clearly states that the /raw/product/ call needs an {Id} property for the Id of the product you wish to retreive.
To get a list of products, use /raw/product/getpaged/
That still requires a parameter, in this case {pagesize}, which means you still need to add a parameter on how many products per page you wish to retreive.
A correct call would be something like "/raw/product/getpaged/10"
You can verify in your browser that trying to "GET" just /raw/product/ will result in the 404 error that you are seeing in your code.
While adding a parameter for the ID result in an "unauthorized access" error, as I do not have your key and secret.
I'm writing an RESTful API which consumes another RESTful Data API, and I'm using Spring Data.
The client side send the page request using query params like:
http://api.mysite.com/products?page=1&size=20&sort=id,asc&sort=name,desc
And I transform the params to PageRequest object and transfer it to the Service Layer.
In the service, I would like to use TestTemplate to interact with the Data API which use a URL, and How can I transform the PageRequest object to a query string like
page=1&size=20&sort=id,asc&sort=name,desc
then I can request data like:
restTemplate.getForEntity("http://data.mysite.com/products?page=1&size=20&sort=id,asc&sort=name,desc",String.class)
I know I am a bit late on this answer, but I too was not able to found an already implemented way to do this. I ended up developing my own routine for doing this:
public static String encodeURLComponent(String component){
try {
return URLEncoder.encode(component, "UTF-8");
} catch (UnsupportedEncodingException e) {
throw new RuntimeException("You are dumm enough that you cannot spell UTF-8, are you?");
}
}
public static String queryStringFromPageable(Pageable p){
StringBuilder ans = new StringBuilder();
ans.append("page=");
ans.append(encodeURLComponent(p.getPageNumber() + ""));
// No sorting
if (p.getSort() == null)
return ans.toString();
// Sorting is specified
for(Sort.Order o : p.getSort()){
ans.append("&sort=");
ans.append(encodeURLComponent(o.getProperty()));
ans.append(",");
ans.append(encodeURLComponent(o.getDirection().name()));
}
return ans.toString();
}
It is not complete, there probably are a couple of details that I am missing, but for my user case (and I think for most of them) this works.
you can pass as new :
new PageRequest(int page,int size)
And in repository layer you can write as:
Page<User> findByName(String name,Pageable page)
in place of Pageable page you have to pass new PageRequest(int page,int size)
For ascending order you can refer this:
List<Todo> findByTitleOrderByTitleAsc(String title);
I think you just need to iterate through the request parameters from your other API request, and then pass all the parameters values into the new Url for a new API request.
sudo code could be:
//all query params can be found in Request.QueryString
var queryParams = Request.QueryString;
private string ParseIntoQuery(NameValueCollection values)
{
//parse the identified parameters into a query string format
// (i.e. return "?paramName=paramValue¶mName2=paramValue2" etc.)
}
in your code, you will then do this:
restTemplate.getForEntity(urlAuthority + ParseIntoQuery(Request.QueryString));
simple as that. Hope that answers?
UriComponentsBuilder uriComponentsBuilder = UriComponentsBuilder.fromHttpUrl("http://data.mysite.com/products");
addRequestParam(uriComponentsBuilder, "page", pageable.getPageNumber());
addRequestParam(uriComponentsBuilder, "size", pageable.getPageSize());
String uri = uriComponentsBuilder.toUriString() + sortToString(pageable.getSort());
addRequestParam method:
private static void addRequestParam(UriComponentsBuilder uriBuilder, String parameterName, Object parameter) {
if (parameter != null) {
uriBuilder.queryParam(parameterName, parameter);
}
}
implement sortToString(Sort sort) method as #Shalen. You have to obtain something like this: &sort=name,asc&sort=name2,desc. Return "" if Sort is null;
I've used Umbraco 7.3 in my project. I created a custom data type but when I want to call a Surfacecontroller in here is HelloSurfaceController or Hello2SurfaceController, I got an error in umbraco backoffice that said Request error: The URL returned a 404 (not found):
I studied some articles about routing but I couldn't solve my problem. I don't know that where I did wrong.
How can I solve this problem?
Reply.controller.js:
angular.module("umbraco")
.controller("Reply.controller", function ($scope, $http) {
$scope.SendReply = function () {
var sendTo = $("#Email").val();
var textMessage = $("#TextMessage").val();
$scope.xxx = "I'm here!";
var data = { SendTo: sendTo, TextMessage: textMessage };
// ~/Hello2Surface/ReplyMessage ---> Cannot find this URL
$http.post("~/App_Plugins/Reply/HelloSurface/ReplyMessage") // Can not find this URL
.then(function (response) {
alert("YES!");
//TODO:
});
}
});
SurfaceController
namespace Jahan.Nuts.Web.Mvc.UmbracoCms.App.App_Plugins.Reply
{
public class HelloSurfaceController : SurfaceController
{
[HttpPost][ChildActionOnly]
public ActionResult ReplyMessage()
{
//TODO: how should be write this method that be proper for getting data from angularjs?
return null;
}
}
}
package.manifest
{
propertyEditors: [
{
alias: "Send.Reply",
name: "Send Reply",
editor:{
view:"~/App_Plugins/Reply/Reply.html"
},
}
]
,
javascript:[
'~/App_Plugins/Reply/Reply.controller.js'
]
}
Reply.html
<div ng-controller="Reply.controller">
<div style="width: 100%;">
<input type="button" value="Send Reply" title="SendReply" name="Send Reply" ng-click="SendReply()" />
</div>
<div>
<input type="text" ng-model="xxx" name="message" />
</div>
Error in umbraco backoffice:
Take a closer look at the documentation - in particular the Plugin-based SurfaceControllers section:
https://our.umbraco.org/documentation/Reference/Routing/surface-controllers
try doing this (note the PluginController attribute):
namespace Jahan.Nuts.Web.Mvc.UmbracoCms.App.App_Plugins.Reply
{
[PluginController("Reply")]
public class HelloSurfaceController : SurfaceController
{
[HttpPost][ChildActionOnly]
public ActionResult ReplyMessage()
{
//TODO: how should be write this method that be proper for getting data from angularjs?
return null;
}
}
}
Other Notes:
You don't need to include "Surface" in the controller name anymore - simply calling it HelloController is enough.
Don't use a SurfaceController for Api calls if you're using it with AngularJS - Better to use an UmbracoApiController instead. Check out https://our.umbraco.org/documentation/Reference/Routing/WebApi/ for more information (including notes on where to expect the Api Endpoint to be)
You might also want to re-locate your controller so it's in a more conventional spot. There's no problem with putting it in the ~/Controllers directory even if it is a Plugin Controller.
Edit: Added "correct" way to do this:
As noted above, to implement an UmbracoApiController, you want a class looking like this - note you can use UmbracoApiController if you don't need to worry about authorization:
namespace Jahan.Nuts.Web.Mvc.UmbracoCms.App.App_Plugins.Reply
{
[PluginController("Reply")]
public class HelloApiController : UmbracoAuthorizedApiController
{
public void PostReplyMessage(string to, string message)
{
// TODO: process your message and then return something (if you want to).
}
}
}
Then in AngularJS set up a resource like this:
function replyResource($q, $http, umbDataFormatter, umbRequestHelper) {
var replyResource = {
sendMessage: function (sendTo, msg) {
return umbRequestHelper.resourcePromise(
$http.post("Backoffice/Reply/HelloApi/PostReplyMessage?" +
umbRequestHelper.dictionaryToQueryString(
[{ to: sendTo }, { message: msg }])),
'Failed to send message to ' + sendTo + ': ' + msg);
}
};
return replyResource;
}
angular.module('umbraco.resources').factory('replyResource', replyResource);
and finally your actual view controller can use this as follows:
angular.module("umbraco")
.controller("Reply.controller", function ($scope, $http, $injector) {
// Get a reference to our resource - this is why we need the $injector specified above
replyResource = $injector.get('replyResource');
$scope.SendReply = function () {
// You really shouldn't use jQuery here - learn to use AngularJS Bindings instead and bind your model properly.
var sendTo = $("#Email").val();
var textMessage = $("#TextMessage").val();
replyResource.sendMessage(sendTo, textMessage)
.then(function (response) {
// Success
}, function (err) {
// Failure
});
}
};
});
It's possible there's some errors in there; I did it mostly from memory - in particular, you may need to look into the best way to post data to the ApiController - it's not likely that it'll just accept the two parameters like that.
For a more complete example, consider reviewing the code of the Umbraco MemberListView plugin: https://github.com/robertjf/umbMemberListView
Also, you really should read up on the ASP.Net MVC fundamentals and the Umbraco Documentation for SurfaceControllers and APIControllers I've listed above already.
remove the "Surface" from the URL and include "backoffice":
angular.module("umbraco")
.controller("Reply.controller", function ($scope, $http) {
$scope.SendReply = function () {
var sendTo = $("#Email").val();
var textMessage = $("#TextMessage").val();
$scope.xxx = "I'm here!";
var data = { SendTo: sendTo, TextMessage: textMessage };
// ~/Hello2Surface/ReplyMessage ---> Cannot find this URL
$http.post("backoffice/Reply/Hello/ReplyMessage") // Can not find this URL
.then(function (response) {
alert("YES!");
//TODO:
});
}
});
Also, I'd recommend using UmbracoAuthorizedController not a surface controller as this is being used in the back end by logged in users it'll be wise to keep it secure.
So instead your controller should look something like this:
[PluginController("Reply")]
namespace Jahan.Nuts.Web.Mvc.UmbracoCms.App.App_Plugins.Reply
{
public class HelloApiController : UmbracoAuthorizedJsonController
{
public [Model-to-be-returned-to-angular] ReplyMessage()
{
//sql query etc to populate model
//return model
}
}
}
I am looking for a way to preserve a url parameter after posting through a form. For example my GET method takes a string "type" and uses that to determine the type of report to render in the View. The url looks like this:
http://mysite/Reports/Report?type=1
[HttpGet]
public ActionResult Report(string type)
{
var model = new ReportsModel()
{
Report = ReportList.Find(o => o.ReportType == type)
};
return View(model);
}
The View has a form that has start/end date filters used to determine the date range of the date to be displayed for the type of report:
#using (Html.BeginForm("Report", "Reports"))
{
Report.ReportName
#Html.HiddenFor(o => o.Report.ReportType)
#Html.EditorFor(o => o.Report.StartDate )<br/>
#Html.EditorFor(o => o.Report.EndDate )<br/>
<button id="reports">Report</button>
}
The above form posts to an action that gets report data from the database based on the specified report type, start/end dates, and returns back to the view.
[HttpPost]
public ActionResult Report(GenericReportsModel model)
{
switch (model.Report.ReportType)
{
case ReportType.ReportType1:
model.Result = ReportRepository.GetReport<ReportType1>(model.StartDate, model.EndDate);
break;
case ReportType.ReportType2:
model.Result = ReportRepository.GetReport<ReportType2>(model.StartDate, model.EndDate);
break;
}
return View(model);
}
The problem is that after the post, the "type" parameter is lost from the url.
Before the post: http://mysite/Reports/Report?type=1
After the post: http://mysite/Reports/Report
I need to be able to do something like this (which doesn't work):
return View(model, new {ReportType = model.ReportType);
How can I preserve the type parameter in the url after the post, in case someone wants to copy and paste the url to send to someone else?
You need to update Html.BeginForm and your HttpPost version of Report method.
#using(Html.BeginForm("Report", "Report", "YourController", new { type = model.ReportType})
{
// I am assuming that model.ReportType == type argument
// in your HttpGet Report action
// The rest of the form goes here
}
Your action should look like:
[HttpPost]
public ActionResult Report(string type, GenericReportsModel model)
{
switch (model.Report.ReportType)
{
case ReportType.ReportType1:
model.Result = ReportRepository.GetReport<ReportType1>(model.StartDate, model.EndDate);
break;
case ReportType.ReportType2:
model.Result = ReportRepository.GetReport<ReportType2>(model.StartDate, model.EndDate);
break;
}
return View(model);
}
If type is not equal to model.ReportType then you should create a ViewModel that contains the values from your GenericsReportModel and this other Report type.
I have a site which is using facebook for auth. I want to gather some basic info when a user signs up including their email address.
The code i have for the login is standard:
public ActionResult Login(string returnUrl)
{
var oAuthClient = new FacebookOAuthClient();
oAuthClient.AppId = AppSettings.GetConfigurationString("appId");
oAuthClient.RedirectUri = new Uri(AppSettings.GetConfigurationString("redirectUrl"));
var loginUri = oAuthClient.GetLoginUrl(new Dictionary<string, object> { { "state", returnUrl } });
return Redirect(loginUri.AbsoluteUri);
}
How do i add the request to access permissions in that? Or do i do it another way?
You need to use the email permission (the full list is here: http://developers.facebook.com/docs/authentication/permissions/ )
The way to add permissions to the authorization is by appending a comma separated list to &scope= , e.g.:
https://www.facebook.com/dialog/oauth?client_id=YOUR_APP_ID&redirect_uri=YOUR_URL&scope=email,read_stream
Update: As you marked, the parameters are passed to the GetLoginUrl() method, although in the codeplex forum they also used ExchangeCodeForAccessToken(), which you might want to take a look at also.
A couple of examples using the C# SDK:
http://blog.prabir.me/post/Facebook-CSharp-SDK-Writing-your-first-Facebook-Application.aspx
Facebook .NET SDK: How to authenticate with ASP.NET MVC 2
http://facebooksdk.codeplex.com/discussions/244568
A snoop at the sdk code and i came up wiht:
public ActionResult Login(string returnUrl)
{
var oAuthClient = new FacebookOAuthClient();
oAuthClient.AppId = AppSettings.GetConfigurationString("appId");
oAuthClient.RedirectUri = new Uri(AppSettings.GetConfigurationString("redirectUrl"));
var parameters = new Dictionary<string, object>();
parameters["state"] = returnUrl;
parameters["scope"] = "email";
var loginUri = oAuthClient.GetLoginUrl(parameters);
return Redirect(loginUri.AbsoluteUri);
}
not tested it yet and the missus is shouting at me for working late so will have to test tomoz :)