Netcore 2.1 OData #odata.context - asp.net-core

How do I retrieve the #odata.context value from a request to a aspnetcore 2.1 using odata v4 (microsoft aspnetcore.odata 7.0.1) inside the controller?
I needed to form my response as
{ "#odata.context":"https://localhost:5001/odata/$metadata#Products/$entity",
"value" :[{ "id":"b79a6aa2-ed65-400c-8f7d-42052412b0b4"}]
}
rather than ``{"#odata.context":"https://localhost:5001/odata/$metadata#Products/$entity","id":"b79a6aa2-ed65-400c-8f7d-42052412b0b4"}
Thank you!!
Alberto

It looks like your controller is returning a single value response, if you have it return an Enumerable then it'll be in the form you want

Related

use fix route in asp.net core api

I'm converting my code to Asp.net core 3.1 in a web api project. my code is something similar to bellow:
When i call http://localhost:44307/PersonDetails I would like to get "Result1" and when i call http://localhost:44307/Person/2207 i should get "result2".
The first route works as i expected but for the second one, I get 404. it works if i call http://localhost:44307/PersonDetails/Person/2207 but it is not what i expected.
Appreciate if you could help me.
Try it like below:
[HttpGet]
[Route("~/Person/{personId}")]
public ActionResult<Person> Get([FromQuery]int personId)
{
return Ok("result2");
}
When you add ~ before it, it will ignore the route attribute on your controller.
[Route("[controller]")]
when you use a route on your controllers, all child actions will use that. so in this case, because your controller name is PersonDetails you have to enter the controller name in your URLs.
but, you can use this trick :
step1: remove this line:
[Route("[controller]")]
step2: add PersonDetails to your Get method
[Route("PersonDetails")]
Public ActionResult Get()

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)) ...

session data is not retrieved in ASP.NET MVC

I have created the internet application ASP.NET MVC Using RAZOR.
I have written in Index.cshtml as:
#Session["my"]="Hello".
and I have written in AboutUs.cshtml as:
#Session["my"]
Still now I can't get the value from AboutUs.cshtml .
Instead of it in Index.cshtml it shows:
="Hello"
Replace #Session["my"]="Hello"; with #{ Session["my"] = "Hello"; }.
Note the addition of curly brackets.

ASP.NET Web API 2 - define controller-to-method mappings?

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.