Cannot POST api call from .net core MVC - asp.net-web-api

I have the following code in my .net core MVC application:
[HttpPost]
public void GetCustomerInfo([FromBody] string value)
{
string data = value;
}
after deploy this to my azure we app and call 'azuresiteurl/api/controller/GetCustomerInfo' page not found error occurs.
Can anyone please help?

you are using a get request but you had defined it as a post request change it as [httpget]

You should pass the string value in the request body and since it is marked as a HttpPost method, it has to be a Post call from the client.
Additionally, check if you are indeed sending the value in the request body. The content type should be coming in correctly for FromBody to work and map the primitive type.
I suggest you go through the answer of this question.
WebApi POST works without [FromBody]?

Related

Dynamic Web API Controllers and WithConventionalVerbs()

I'm using Dynamic Web API Controllers but my Get methods are being created as POST.
evidence
I've already set ".WithConventionalVerbs()" but no success
Configuration.Modules.AbpWebApi().DynamicApiControllerBuilder
.ForAll<IApplicationService>(typeof(AssisteVidaApplicationModule).Assembly, "app")
.WithConventionalVerbs()
.Build();
My Put and Delete methods are okay.
What's wrong?
The only thing you could miss; there could be [HttpPost] attribute on PedidoAppService.Get() and PedidoAppService.GetAll() methods. Can you check if there's any HttpPost attribute on these methods? And to understand the problem you can rename the Get method to GetSomething() ... if http verb gets correct we can have more info about the problem.

WebApi Http Error 404 Not Found

I'm having a problem with WebApi 5.2.3 in which if I had a controller like so:
[RoutePrefix("api/values")]
public class ValuesController : ApiController
{
[Route]
public IHttpActionResult Get()
{
return Ok("data");
}
}
Currently if i make a request to api/values I retrieve my response "data" just fine. I would like to handle the case of routes that are not found, for example "api/values/foo". Currently making the api/values/foo request returns the typical IIS Http Error 404.0 Not Found page. I would like to be able to handle this and return a json or xml response based on the negotiated content type. Has anyone ran into this before and how did you solve it?
Thanks in advance.
Also to note, I created a DelegatingHandler and confirmed that my request is not entering the WebApi stack. Any ideas?
You might need to create your custom controller/action selector (IHttpControllerSelector/IHttpActionSelector), take a look into this article which creates a custom one to handle the 404 error.
http://weblogs.asp.net/imranbaloch/handling-http-404-error-in-asp-net-web-api
Hope this helps.

Parameter format issue with ASP.NET Web API controller

I am watching this pluralsight demo video on Web Api, and he is using fiddler to pass in a parameter using Http Get with the syntax of controller/parameter
So he's using http://localhost:2405/api/values/5
5 Is the parameter he's passing in.
In my code, I have everything set up exactly the same way he does... with a routing template of {controller}/{id} and a controller method with a signature of
public string Get(string zipcode)
I can pass a parameter just fine with http://localhost:2405/api/values?zipcode=25252 but if I try passing in a paramter the way he does, like http://localhost:2405/api/values/25252 I get an error saying I do not have an action available to handle that request on the controller.
What is he doing right, that I'm doing wrong?
You need to change your routing template to {controller}/{zipcode} as the name of the parameter must match the name in the template.

How to use Breeze IQueryable with CORS?

I use a method to add CORS handlers to my response that is called by a client using Breeze.
You can read more about how I got that working here: Controller not filtering data in Breeze query in DotNetNuke Module
However, I noticed that while $filter works, $expand and $select do not.
So my question is: How can I use return a HttpResponseMessage Type and still use Breeze (I need to do this for CORS).
To prove this, I downloaded and changed the Todos sample:
Original method (works)
http://example/api/todos/todos?$select=isdone
[HttpGet]
public IQueryable<TodoItem> Todos()
{
return _contextProvider.Context.Todos;
}
My method with CORS wrapper (does not expand or select)
http://example/api/todos/TodosCors?$select=isdone
[HttpGet]
[Queryable(AllowedQueryOptions = AllowedQueryOptions.All)]
public HttpResponseMessage TodosCors()
{
var response = Request.CreateResponse(HttpStatusCode.OK, (IQueryable<TodoItem>)_contextProvider.Context.Todos);
return ControllerUtilities.GetResponseWithCorsHeader(response);
}
public static HttpResponseMessage GetResponseWithCorsHeader(HttpResponseMessage response)
{
response.Headers.Add("Access-Control-Allow-Origin", "*");
return response;
}
I'm going to comment mainly on the CORS aspect of your question. The part about $expand and $select is addressed in the StackOverflow question to which you refer. In brief, [Queryable] is the Web API attribute which does not support $expand and $select. I think you want the [BreezeQueryable] attribute that does.
I can not say for sure but I do not believe the code you show is the proper way to implement CORS for the Web API. At least I've not seen it done this way.
There are two ways known to me; both involve adding message handlers.
The first is the way we did it in the Breeze Todo sample; the second is the with the Web API CORS support that is on the way.
The way we did it is simplistic but effective. We don't talk about it because we intend to defer to the the approved Web API way when it arrives (soon I hope).
In the Todo demo, look for App_Start/BreezeSimpleCorsHandler.cs. You can just copy it into your own App_Start folder with no changes except to the namespace.
Then your server has to call it. In the Todo sample we did so in the BreezeWebApiConfig.cs but you could put it in Global.asax or in anything that is part of the server boot logic.
// CORS enabled on this server
GlobalConfiguration.Configuration.MessageHandlers.Add(new BreezeSimpleCorsHandler());
As it happens, someone has tried Breeze with the forthcoming Web API CORS NuGet package ... and discovered a bug in Breeze. We have to work that through ... and we will. We really want that way to be THE way.
Until then, you can follow the Todo sample precedent.

Web API get route values

Is there any way to statically get route values from a service method (outside of a controller) that is running in a Web API context? For example, I can do the following in ASP.NET MVC:
var mvcHandler = HttpContext.Current.Handler as MvcHandler;
var routeValues = mvcHandler.RequestContext.RouteData.Values;
I'd like to find the equivalent version of this code for Web API.
When I try to debug a sample Web API request and look at HttpContext.Current.Handler it is of type HttpControllerHandler, but this type doesn't have any properties to access route data.
EDIT
To try to help provide some more information. The code I am trying to read the value from is inside of a factory class I have that builds a custom object for my application.
You can use GetRouteData() extension on HttpRequestMessage. You would need to include System.Net.Http namespace to get this.
System.Web.Http.Routing.IHttpRouteData routeData = Request.GetRouteData();
I was able to find a solution that would get the route values for either an MVC request or a Web API request.
HttpContext.Current.Request.RequestContext.RouteData

Resources