I added a route to my asp.net mvc application to handle some json requests I need to do. This works great on my development pc, however when I installed in QA, the route isn't working at all. I tried to physically type in the address and get a "Bad Request". Can anyone assist with this? I have restarted IIS to try to clear any cache but still no luck. First time, I've seen this as I've made several changes to routes before.
routes.MapRoute(
"FsrProblemTypesByEquipment",
"Fsr/ProblemTypesByEquipment/{equipmentID}",
new {controller = "Fsr", action = "ProblemTypesByEquipment", equipmentID = ""});
Do you still get a "bad request" if you take out the new route entry, but use the same test URL? If it is really a problem with your routing table, then your catchall route should get the request.
My guess is that the request is never getting to your routing table; IIS is catching the request before it gets passed to your application. If that is true, then it is a configuration problem in IIS, or IIS is unhappy with the construction of the URL for some reason.
See this forum post for more information:
http://forums.asp.net/p/1458130/3343674.aspx
Related
I have a web service deployed on IIS 8.5 (Windows Server 2012 R2) which has to be called passing four arguments. They are not optional, and are to be included directly in the URL, without a query string, as such:
.. api/myservice/argument1/argument2/argument3/argument4
If I try to call it with the URL written above, it answers and gives the expected response. However, a 404 - Not Found error is given instead when trying to use real production-like arguments as such:
api/myservice/AAAA_AAAAA.AAA_AAA_AA_AA_AAA_000000_000000_000000/333/AAA/AAA.AAA.AA.AA.AAA.000000.000000.000000
I thought the multiple dots raised the issue, so I replaced each one of them with %2E, but nothing changed.
I've searched for already answered questions: I tried this and this, to no avail.
What is the problem with that production-like URL?
Here is the code of the API:
[RoutePrefix("api/myservice")]
public class MyServiceController : ApiController
{
[HttpGet]
[Route("{argument1}/{argument2}/{argument3}/{argument4}")]
public string StartValidation(string argument1, string argument2, string argument3, string argument4)
{
// operations...
}
}
I traced the request with IIS Failed Request Tracing as suggested in an answer, but I'm not able to find a clue in the resulting log:
Can anyone help me?
Enable failed request tracing on IIS to check.
https://learn.microsoft.com/en-us/iis/configuration/system.applicationhost/sites/site/tracefailedrequestslogging
Good possibility that IIS is rejecting these as part of URLScan (if enabled), but FRT will confirm.
First try to deploy it to your local Iis, if it works, means that your production can have a different configuration at the level of the IIS, or the compiled code is not the same as the one you have and you need to redeploy the api.
If the api fails at the level of your local IIS and it runs properly from the vs(if you pass any parameter with the postman) you hit a backend breakpoint, then you have a wrong configuration on your deployment on your local IIS.
If you never hit the breakpoint you have an error at the level of your code.
Hope this helps
I finally solved this. I added a piece code, provided by this answer to a quite identical question, to the web.config, but a different issue arose: it seemed IIS had to be run in "Integrated Mode". Thanks to the clue given by this answer to a question about this subsequent problem, I switched the Managed pipeline mode of the application pool where the API was deployed from Classic to Integrated, which allowed that code to work. The web API finally accepts arguments with periods.
With the help of several online tutorials, like this one, I am still struggling to add a Web API service to an existing Asp site, that is not MVC.
I added to the project a new item of type Web API Controller Class(v2.1), named it something like AbcController.cs, and VS2015 asked me to put it in the App_Code directory. The default code has handlers for Get, Put etc. Sounded to me like I am on the right track.
I added a default route in Global.asax.cs like in the tutorial:
RouteTable.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
This got built after adding a reference to System.Web.Http.Webhost which was not mentioned in the tutorial. Sounded like I was still on the right track.
However, it doesn't work. I run the site in debug and this gives me a 404 Not Found:
http://localhost:54905/api/abc
I tried to run this on the production server with IIS7, of course as a second test web site to not interfere with the version that is in production. However, I ran into the error that the Microsoft.Web.Infrastructure dll could not be found. To fix this, I should install MVC packages, which I don't like for just an experiment.
My questions are:
do I get it right that the URL is in lower case, i.e., not .../api/Abc ?
does this kind of routing work in the debugger?
am I essentially turning the web site into an MVC web site?
is this really the simplest way to add a "REST" service to an existing web site? I only need to implement the POST, read and return some JSON data, and do not need arguments in the URL
So I added an ASMX web service to my MVC4 but when I tried to access it I got a "The Resource could not be found" error. After searching I found the answer here.
In short, I had to add the following IgnoreRoute to my RouteConfig file.
routes.IgnoreRoute("{*x}", new { x = #".*\.asmx(/.*)?" });
I understand the MapRoute function in MVC fairly well, however the IgnoreRoute, not so much. I understand that it's targeting the .asmx postfix but I'm not sure on the how's and why's of this quick fix.
How does this IgnoreRoute work, and exactly why does it make my MVC app magically understand how to find and execute my web service? BTW, My only mapped route, currently, is the default, but is there another/better way of solving this issue using MapRoute or another fix?
ignore route indicates that routing should ignore these requests and ASP.NET processing of these requests will occur.
http://haacked.com/archive/2008/07/14/make-routing-ignore-requests-for-a-file-extension.aspx/
I am using routing in webforms to clean up urls. I store all my .aspx files in the /Pages directory and route to them with clean URLs. As it stands I can still access these files by navigating to /Pages/Home.aspx for example. How can I either throw a 404 or redirect to a different page in my routing table? Here is what I have tried:
routes.RouteExistingFiles = true;
routes.Ignore("{resource}.aspx/{*pathInfo}");
routes.Ignore(#"*\.aspx");
routes.MapPageRoute("Home", "", "~/Pages/Home.aspx");
This isn't working at all, and I could use some advice.
If I try accessing a physical file on my MVC site, I get the following:
Object reference not set to an instance of an object.
Can I do this on webforms?
routes.RouteExistingFiles = true;
routes.MapPageRoute("Route1", "Pages/{page}.aspx", "~/404.aspx");
routes.MapPageRoute("Route2", "Pages/{folder}/{page}.aspx", "~/404.aspx");
Instead of Ignoring the route, you need to map it to a page that throws a 404 instead of a response. Hopefully this helps someone else!
I just recently got a fresh install of Windows 7 on a my PC running VS2012/VS2010. I have an MVC3 project that ran just fine before I pulled it onto this PC to run. The project still compiles on this PC and I can navigate through my site while running the app in studio (2010 or 2012), but when I tried to POST from ANY form in ANY view and pass an ID by means of the URL like this:
<form id="ScriptForm" action="/MyApp/ControllerName/ActionName/#ViewBag.IDNumber" method="post">
...
</form>
...I got this error. I did some digging and playing around to try to fix this, ultimately taking these steps to try to resolve:
Actually installed ASP.NET via the Windows Components form in Control Panel
Changed the application to use IIS Express
Well, when I changed it to run under IIS Express, I started getting a different error.
HTTP Error 404.0 - Not Found
The resource you are looking for has been removed, had its name changed, or is
temporarily unavailable.
Most likely causes:
The directory or file specified does not exist on the Web server.
The URL contains a typographical error.
A custom filter or module, such as URLScan, restricts access to the file.
Things you can try:
Create the content on the Web server.
Review the browser URL.
Check the failed request tracing log and see which module is calling SetStatus. For more information, click here.
Detailed Error Information:
Module IIS Web Core
Notification MapRequestHandler
Handler StaticFile
Error Code 0x80070002
Requested URL http://localhost:51596/MyApp/ControllerName/ActionName/1
Physical Path C:\CODE\MyApp\ControllerName\ActionName\1
Logon Method Anonymous
Logon User Anonymous
Request Tracing Directory C:\Users\cbarlow\Documents\IISExpress\TraceLogFiles\MYAPP
More Information:
This error means that the file or directory does not exist on the server. Create the file or directory and try the request again.
View more information ยป
It's almost as if it is not recognizing that this is a route and is trying to resolve the URL to a physical file (like 1.html) which obviously does not exist. But why isn't it "doing the MVC thing" and using the route? I have this in my global.asax:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
}
And I know this is running because I can breakpoint it.
Specs:
Windows 7 | Visual Studio 2010/2012 | Microsoft MVC3 | IIS Express
I've read all these SO posts, none seem to apply in this situation or do not help (mostly, because they apply to actual aspx pages, where I am trying to load pages via controllers):
The HTTP verb POST used to access path '/' is not allowed
The HTTP verb POST used to access path is not allowed
The HTTP verb POST used to access path '/Membership/user/' is not allowed
The HTTP verb POST used to access path '/Main/[object Object]' is not allowed
The HTTP verb POST used to access path '[my path]' is not allowed
HTTP verb POST used to access path '/' is not allowed in Facebook app
Any ideas?
The problem is here (due to my lack of experience with MVC/CSHTML):
<form id="ScriptForm" action="/MyApp/ControllerName/ActionName/#ViewBag.IDNumber" method="post">
...
</form>
The MyApp used to work on my PC (I'm honestly not sure why it doesn't work now... maybe I had set something up for that name to resolve before?) but it no longer maps to anything. It DOES work on the server, but that's because there is indeed a mapping for "MyApp" in IIS. Simply changing this form to this:
<form id="ScriptForm" action="#Url.Content("~/ControllerName/ActionName/" + ViewBag.IDNumber)" method="post">
...
</form>
...does the job more elegantly and without errors.