FromForm Swashbuckle and ModelBinder - asp.net-core

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?

Related

Dot.NET Core Routing and Controller Processing discrepancy

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)

Are FromUri and FromQuery the same?

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.

Obtain strongly typed header class in ASP.NET Core

How do you obtain a strongly typed header class from the namespace System.Net.Http.Headers from an ASP.NET Core controller? In a controller derived from Controller, Request.Headers is available, but it just returns IHeaderDictionary. There is also an extension method HeaderDictionaryTypeExtensions.GetTypedHeaders, but it returns RequestHeaders, which has only certain headers. The class HttpRequestHeaders has the most comprehensive list of headers, but it's not clear how to access it.
For example, how would you get a AuthenticationHeaderValue? One option is AuthenticationHeaderValue.Parse(Request.Headers["Authorization"]), but that requires hard coding the header name. Perhaps there is a non-hard-coded way to get to HttpRequestHeaders.Authorization.
Use AuthenticationHeaderValue to parse the header string into an object with Scheme and Parameter properties.
var auth = AuthenticationHeaderValue.Parse(Request.Headers[HeaderNames.Authorization]);
if (auth.Scheme != expectedScheme || !MyVerifyAuthParamteter(auth.Parameter)) ...

Can we send multiple Properties in Json of Analytics(IBM MobileFirst)

I was asked for a usecase where I have to filter ActionEvents on type of Page that action is being is being called from.
Example
use case: I have a login page and I have to capture analytics of its events
Can I do something like this
String json = {"PageLevel":"LoginPage","ActionLevel":"LoginButton"};
WLAnalytics analytics=new WLAnalytics();
analytics.log(message, new JSONObject(json));
Will this work... can we create custom chart with first property being ActionLevel and filter it as per PageLevel.
No this will not work. Custom analytics can only be logged in key value pairs i.e. you cannot send two key value pairs in one JSON object. This is a good idea though, I recommend you submit an RFE.
Submit a Feature Request

How to enable dojox.data.JsonRestStore access struts2's action to retrieve data? I mean how to configure 'target' or others

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.