I'm familiar with FromBody and FromRoute. They seem to be clear.
I've used FromUri to handle multi-valued parameters mapping to a list or a string[].
FromQuery sounds similar, but is there any difference?
[FromQuery] attribute handles query parameters, i.e. key-value pairs coming after "?" in URI.
[FromRoute] attribute handles route parameters coming before "?" in URI, i.e. path parameters.
For example, if you configured route "orders/{id}", then "id" is your route parameter, and if some actual request is like "orders/123?showHistory=true", then "showHistory" is your query parameter.
[FromUri] attribute in Web API works like [FromQuery] in ASP.NET Core MVC.
Related
I'm using JsonConverter for ID properties to convert them back and forth using hashids (when receiving request - from hashed string to int64, when sending response - from int64 to hashed string).
That all works flawlessly but I'm having now a problem with routes that receive Form Data. I tried using ModelBinder for ID properties but then swashbuckle generates that action with ID property outside request body, it defines it as a query parameter. Is there any other way to convert form data properties like JsonConverter is doing doing so for JSON?
Thanks
/edit: more info
I want swagger to generate schema like this where I use ModelBinder for Id property
Current:
Wanted:
Beside swagger schema, I want Id property to have ModelBinder (or if there's any other way) to have it convert it from string to int64 before it calls service to update user.
I have tried only ModelBinder for converting itself, as I'm not aware of anything else capable to do that, and for swagger scheme I tried applying IOperationFilter without success. I'm guessing I would have to alter scheme and remove Id Parameter and add it inside Request body instead?
I am trying to use the asp-route- to build a query string that I am using to query the database.
I am confused on how the asp-route- works because I do not know how to specifically use the route that I created in my cshtml page. For example:
If I use the old school href parameter approach, I can then, inside my controller, use the specified Query to get the parameter and query my database, like this:
If I use this href:
#ulsin.custName
then, in the controller, I can use this:
HttpContext.Request.Query["ID"]
The above approach works and I am able to work with the parameter. However, if I use the htmlHelper approach, such as:
<a asp-controller="Report" asp-action="Client" asp-route-id="#ulsin.ID">#ulsin.custName</a>
How do I get that ID from the Controller? The href approach does not seem to work in this case.
According to the Anchor Tag Helper documentation, if the requested route parameter (id in your case) is not found in the route, it is added as a query parameter. Therefore, the following final link will be generated: /Report/Client?id=something. Notice that the query parameter is lowercase.
When you now try to access it in the controller as HttpContext.Request.Query["ID"], since HttpContext.Request.Query is a collection, indexing it would be case-sensitive, and so "ID" will not get you the query parameter "id". Instead of trying to resolve this manually, you can use a feature of the framework known as model binding, which will allow you to automatically and case-insensitively get the value of a query parameter.
Here is an example controller action that uses model binding to get the value of the query parameter id:
// When you add the id parameter, the framework's model binding feature will automatically populate it with the value of the query parameter 'id'.
// You can then use this parameter inside the method.
public IActionResult Client(int id)
Is there a way to pass a specific UI language to the registration page? This is coming from the website and I want it to be the defaut option.
you can send the culture with these headers
c=...
uic=...
https://github.com/aspnetboilerplate/aspnetboilerplate/blob/dev/src/Abp.AspNetCore/AspNetCore/Localization/AbpLocalizationHeaderRequestCultureProvider.cs#L12
and for MVC use culture parameter like below
/register?culture=tr
must be the first parameter of the query string
and last option; you can always override AbpUserRequestCultureProvider
https://github.com/aspnetboilerplate/aspnetboilerplate/blob/dev/src/Abp.AspNetCore/AspNetCore/Localization/AbpUserRequestCultureProvider.cs
UPDATE:
According to the implementation it accepts query string parameters as culture like below
?culture=es-MX&ui-culture=es-MX
See https://github.com/aspnetboilerplate/aspnetboilerplate/issues/2103
If you look at the request headers sent by the browser, it includes "Accept-Language". It can look something like this:
en-US,en;q=0.9,es-419;q=0.8,es;q=0.7
Generally, the preference runs in descending order, so here, the browser is saying it prefers U.S. english before anything else. More here about what the q values mean: What is q=0.5 in Accept* HTTP headers?
You can access this value through in the controller.
Request.Headers["Accept-Language"]
Microsoft has a good tutorial for getting your first ASP.NET Web API project going:
http://www.asp.net/web-api/overview/getting-started-with-aspnet-web-api/tutorial-your-first-web-api
...but what's unclear to me -- how is the mapping between the API URIs and my C# controller methods defined?
for ex:
/api/products/{id}
resolves to
public IHttpActionResult GetProduct(int id)
{
[...]
}
...but im not sure how. automagic?
I ask because I want to create a new mapping for this URI:
/api/setReportNotificationsAsRead?uid={username}&items={itemIDs}
to this new method in my controller:
public IHttpActionResult SetReportNotificationsAsRead(string username, string itemIDs)
{
[...]
}
...but im not sure how. automagic?
The Automagic lies with HTTP Verbs in Web API. The route /api/products/{id} can map to anything that starts with GET (or just method name Get) if you make a GET request for the WebAPI.
If you make a POST request then it will map to any POST method (or else is you use route prefix).
This link can help you understand the magic. That can also help you understanding how you can configure SetReportNotificationsAsRead function to be called.
You're very close to having a working solution. You have your method parameter names mixed up:
public IHttpActionResult SetReportNotificationsAsRead(string uid, string items)
{
//Do your stuff!
}
The name of the HTTP GET parameter should match the name of the method parameter. This is part of the ASP.NET MVC naming convention.
One final note, if you are actually expecting a group of items (i.e. multiple item ids) you can actually save yourself some work and use a string[] parameter so long as your HTTP parameters are sent correctly. In this case you would want the URI to look like: /api/setReportNotificationsAsRead?items=123&items=456&items=789. Notice how the parameter name is repeated multiple times, but with different values.
For more information about the routing system I would suggest heading over to the ASP.NET MVC routing overview.
I tend to use dojox.data.JsonRestStore as my grid's store, but I am always failed to access struts2 action, I am unfamiliar in REST, is it only can be used in servlet rather than struts2, etc.
Currently, My project is using struts2 + spring as backend skill and dojo as front-side skill, have you any ways for me to make dojox.data.JsonRestStore access a structs2 action class?
Thanks in advance.
to get the data, all you need is an HTTP GET that returns an array of JSON objects. The return value from the action must be a string with something like:
[
{
"penUser":"Micha Roon",
"submitTime":"12.03 13:20",
"state":"Eingang",
"FormNumber":"001001"
},
{
"penUser":"Micha Roon",
"submitTime":"12.03 13:20",
"state":"Eingang",
"FormNumber":"001001"
}
]
If you want to be able to update objects you have to have a method that reacts to PUT with the same URL as the one you used for GET and if you need to delete, DELETE will be used. The important part is that it must be the same URL.
In order to have JsonRestStore pass the ID in a GET parameter instead of appending it to the URL, you could specify the URL like so:
target:"services/jsonrest/formstore?formId="
When you call yourStore.get("123") the request will try to get http://yourserver:port/AppContext/services/jsonrest/formstore?formId=123
REST is nothing more than a convention.
You can use a RESTFull API like jersey.java.net in order to make your life easier and your URL more RESTFull.