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/
Related
How can I redirect all not found page requests to the same view?
This illustrates somehow what I mean:
config.RouteTable.Add("Home", "*", "Views/Home.dothtml", new { });
The idea behind is that when someone accesses a not existing page:
http://localhost/WhatEverNonExistingPage
The resquest is redirected to
http://localhost/Home
The only way I found so far is to implement it outside the dotvvm rules:
appBuilder.Run(context =>
{
context.Response.Redirect("/Home");
return Task.FromResult(0);
});
This means that basically any request not matching any route in the configured middlewares will be redirected to '/Home'. Not exactly the answer I was looking for, but it is effective.
Since DotVVM is Owin you can use it with Nancy. Any routes that aren't matched in DotVVM get passed on to Nancy. You can then use Nancy to handle your 404's or any other types you may need such as 301's if you are rebuilding a site.
It's a somewhat long-winded approach but if you are writing a hybrid app with micro services then it might make sense.
Setting up:
https://github.com/riganti/dotvvm-samples-combo-with-nancy
Handling custom 404's in Nancy.
https://blog.terribledev.io/custom-error-pages-in-nancy/
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
I'm trying to follow the SignalR quickstart sample, but I can't get it to work.
Firstly I'm not quite sure where to place the chat.cs hub file in a MVC project - I have tried a few places, but suspect this is where I'm going wrong. It's currently sitting in a folder called signalr in the project root.
The javascript error I'm getting is as follows:
GET http://localhost:50109/signalr/hubs 404 (Not Found)
I added the signalr nuget package, so I think I should have everything I need!
Thanks for reading
Ah, found the following in the FAQ
Why does signalr/hubs return 404 or Why do I get 'myhub' is undefined?
First, make sure you have a call to MapHubs() in Global.asax
RouteTable.Routes.MapHubs();
I also had to remove the following from my global.asax:
BundleConfig.RegisterBundles(BundleTable.Bundles);
Guess I need to spend some time re-jigging my bundles so to say.
I have a Url like this
http://localhost:4737/Site/listing/NH/Plaistow/2831516
and I want it to reroute to
http://localhost:4737/Site/listing.aspx
I was reading how to do this for Web Forms here
https://web.archive.org/web/20211020111718/https://www.4guysfromrolla.com/articles/012710-1.aspx
Here's what my route looks like.
routes.MapRoute(
"FriendlyUrl",
"Site/listing/{state}/{town}/{mlsnumber}",
"~/Site/listing.aspx");
In my listing page I plan on accessing the following variables
Page.RouteData.Values["state"]
Page.RouteData.Values["town"]
Page.RouteData.Values["mlsnumber"]
But when I navigate to http://localhost:4737/Site/listing/NH/Plaistow/2831516,
I just get a HTTP 404 error.
I know how to get this working with MVC, but this is a fairly large application, all written with web forms, so rewriting isn't feasible.
Any ideas on how to troubleshoot this would be helpful.
Thanks !
Here is the working code. Thank you to mrchief for helping me resolve this.
routes.MapPageRoute(
"FriendlyUrl",
"listing/{state}/{town}/{mlsnumber}",
"~/listing.aspx");
Yore doing it the other way. If you're using WebForms, you need to implement UrlRoutingModule as shown here: https://web.archive.org/web/20201205221404/https://www.4guysfromrolla.com/articles/051309-1.aspx
The Routing Rules were designed for use in ASP.Net MVC applications where you redirect a Url to its appropriate Controller (Page in WebForms) with action params (query params in WebFroms parlance).
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