While trying to pass a JSON data from script side to the method inside WebAPI service we are getting the following exception.
{
"Message":"No HTTP resource was found that matches the request URI '.../api/Values/Sample'.",
"MessageDetail":"No type was found that matches the controller named 'Values'."
}
IMPORTANT NOTE:
While using WebAPI in .net 4.0 and VS2010 we didn’t experience any such issue but when we use WebAPI in .net 4.5 and VS2012 we saw the issue. So kindly diagnosis this with 4.5 framework.
We have attached the sample in the following link. If you could review and post the correct sample with comments it would be really helpful as we are new to WebAPI concepts. Event posting code snippet with proper comments would be fine.
SAMPLE LINK: http://www.fileconvoy.com/dfl.php?id=gb197faeca5017d489993001280c7f122f64fc06a7
Thanks in advance.
I have looked at your sample. Couple of things to fix it:
1.Change the controller name from ValuesController1 to ValuesController. Web API by default looks for controller classes with suffix 'Controller'.
2.Change your route to the following:
System.Web.Routing.RouteTable.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}", defaults: new { id = RouteParameter.Optional});
Related
Using MS Visual Studio 2022 on Windows 10 pro to build an ASP.NET Core MVC for the first time. When this tutorial (https://learn.microsoft.com/en-us/aspnet/core/tutorials/first-mvc-app/start-mvc?view=aspnetcore-6.0&tabs=visual-studio) has me run it, Localhost comes up with some stuff relating to the app (its name, a welcome message). The tutorial says to then add "/MvcMovie/" to the URL in the address field. Then, localhost says "No webpage was found for the web address: https://localhost:44379/MvcMovie/".
I'm new at this. Not sure from the tutorial when/if to actually add something to localhost.
Any help will be appreciated.
If you are refering to this:
In the Configure your new project dialog, enter MvcMovie for Project
name. It's important to name the project MvcMovie. Capitalization
needs to match each namespace when code is copied.
The tutorial is saying that your Project's name should be MvcMovie when you create it. Other than that, the tutorial doesn't ask you to go to localhost/mvcMovie. To figure out the routing in an MVC check the next few parts about Controllers-Views-Models. There will be explained about the routing part and how you can access different parts of the URL.
The MvcMovie mentioned in the document does not require you to enter in the URl, it is just the name of the created project:
If you need to use MvcMovie as the routing address in the Url, you can do this:
Controller:
[ApiController]
public class TestController : Controller
{
[HttpGet]
[Route("MvcMovie")]
public IActionResult MvcMovie()
{
return View();
}
}
View demo:
<h1>This is MvcMovie Controller!</h1>
Test url:https://localhost:7040/MvcMovie
Result:
I think you need to know the routing rules in mvc, you can read this document:
Routing to controller actions in ASP.NET Core
I have an mvc site deployed at godaddy server. The mvc routing is working fine on my system and i tried to deploy it on my free azure subscription to test. Its working there. My route.config file has nothing new:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
On server, i have a virtual directory named "httpdocs". None of the controller is receiving my call and giving error 404 page not found. Can you please assist as to wat other changes do i need to check as it seems related to deployment on godaddy server. Please feel free to let me know if any other information seems missing here so that i can provide additional details.
Ok, After spending almost 2-3 days on it, i finally figured it out. My application is using MVC 5.2 Version and goDaddy provides support for max MVC 5 version.
So after downgrading MVC FROM 5.2 to 5 version, it worked.
This may help someone else who might face this issue.
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]?
I'm getting a 404 not found error when trying to access one of my Web API actions:
I have in my WebAPIConfig:
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{action}/{id}",
defaults: new { id = RouteParameter.Optional }
);
I have a controller named OnSourceAPIController and a GET action ValidateServiceRequestAuto.
I get a 404 error when I try to go to:
http://localhost/api/OnSourceAPI/ValidateServiceRequestAuto
My first guess was I had some kind of routing error, but I created an mvc controller and an index page for it and was able to browse there normally via this routing (api/controller/action). The builtin api/help page even shows /api/OnSourceAPI/ValidateServiceRequestAuto as the route to my api call.
I also tried using the WebAPIRouteDebugger tool. It gives me a 000 404 not found error when I use
http://localhost/api/OnSourceAPI/ValidateServiceRequestAuto
, but actually give me the normal route information if I take localhost off of the front and just use
api/OnSourceAPI/ValidateServiceRequestAuto as the URL...
Any pointers would be appreciated.
I figured it out and feel a little bit silly now.
I have my webapi project running inside of my normal website project with a path of .../api, so because I set up my api routing to be api/controller/action, the path I really need to use is localhost/api/api/controller/action. So it turns out that the 404 was there because it really couldn't find anything at that location. I hate it when computers do exactly what you tell them to.
In my case it was address of created service: WebApp.Start(address)
Should be like: http://localhost:9000/
NOT: http://localhost:9000/api/
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