Parameter format issue with ASP.NET Web API controller - asp.net-web-api

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.

Related

MethodNotAllowedHttpException when using Route::delete

I don't know why I get this
I tested the route with
Route::delete("/attachments/{url}","TrixAttachmentController#destroyAttachment");
localhost:3000/api/v1/attachments?url=hDXtilCleTWc6WyiMeWpp9O0xIx3cRyJuEwVPxzL.jpeg
public function destroyAttachment($url){
dd($url);
}
still i get it
localhost:3000/api/v1/attachments?url=hDXtilCleTWc6WyiMeWpp9O0xIx3cRyJuEwVPxzL.jpeg
here ?url it shows it now using get request
call this route via delete method
can you show form
When using the Route::delete, the request is expecting a POST request with a method of DELETE.
If you are hitting the route in the browser using a GET request, this is the sort of error that will be thrown.
If you are calling the route in a browser you need to change that.
Make sure you are calling it on a form using DELETE verb.

auth() is not working in controller when coming from vue

I have a store method in a controller. now for a field, I am inserting auth()->user()->id. when I am submitting the form from a normal blade, it works fine. but when I am submitting the form from a vue component, it is returning 500 error when I am using auth()->user()->id. instead of this, if i write just 4 or 10 or any static data, it is working fine again!
Maybe the id you submit is in an invalid format and your backend script fails on it. Try to see first if you really have the id and not null or undefined. If so, then see if the id is int or string in your js and then finally see what datatype your backend accepts. If this does not work, please provide some code. You can also see your apache log, what error it throws when you try to access your endpoint.

Cannot POST api call from .net core MVC

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]?

Multiple request params with Locale 'language' param

I'm developing Spring MVC application. I have several locale languages and when I change them on JSP pages which require their own request params, I get an exception like:
Required int parameter 'remedyId' is not present
The link I used to change locale is:
<a href="?language=en">
When I click the link, it overrides request parameters in current URL with it's own.
http://localhost:8080/medapp/remedyInfo?language=ru
Instead of:
http://localhost:8080/medapp/remedyInfo?remedyId=1&language=ru
I suppose there is syntax to add multiple request parameters to current URL, but I don't know it. Thanks.

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