ASP NET MVC custom route fallback - asp.net

I'm wondering if it is possibile to customize routing in a way that all requests are evaluated by a piece of code, redirected to the relevant controller if a match is found or passed to next rout in list if not found.
sample request:
/my coolpage/another one
the code searches and determine that the right controller for this is
Page, action is "list" and id is "123" and so redirects
another request:
/products/list/5
code finds no match al passes it back to next route that knows how to handle it...
any idea on how to do this?

Custom Route class
If you really need this kind of request mangling and you can't do it with IIS URL Rewriting module, then writing your own Route class is your best bet. You will probably have to write some other parts as well, but a custom Route class will be your starting point.

Related

Unable to get ASP.NET MVC and Web API routing straight in my mind - getting 404s galore

I am struggling with routing issues in my ASP.NET MVC + WebAPI app. I feel like I have a moving target here because when I add or change one route I break another. Here is my specific example.
First, my controllers, so you can see the names:
Next, my MVC routing config (I understand that my config is probably duplicative, but it is because I am trying things):
And my Web API routing config (I understand that my config is probably very duplicative, but it is because I am trying things):
As a routing problem example, here's the PodcastFeeds API controller:
So I post to the Create action method...
And as you can see I get the error: 404 not found - "No HTTP resource was found that matches the request URI.
I would love some direction here...
The order or registering routes is important since requests are handled by the firs route that matches the patter. Because your default route is the first one it always selected. You need to put your default route to be the last one and register all your routes starting from the most constraining and ending with default one. Also Is I see in your screenshots some of your routes are not fully configured: for example api/PodcastFeeds route doesn't specify a controller (it needs to look similar to your FeedRouteMap route from the second screenshot):
routes.MapHttpRoute(
name: "PodcastFeeds",
url: "api/PodcastFeeds/{action}/{id}",
defaults: new {controller="PodcastFeeds", action="Create", id=UrlParameter.Optional})
As an alternative you can use attribute routing to avoid those kind of issues.

How to enable/use cross-origin resource sharing with MVC 3?

I want to submit a form using AJAX to a MVC 3 Controller.
The form and the controller are on two different domains, which is why i want to use CORS.
I have read that the following code should do the trick in ASP.NET:
Response.AppendHeader("Access-Control-Allow-Origin", "*");
from http://enable-cors.org/#how-asp.net
Should this code go directly in the controller that takes the form data?
As far as i know, there has to be some exchange of data between the client posting data and the server, to determine wether CORS is enabled/supported or not, so i figure that the one line of code has to go somewhere else?
Thanks
This could go in the controller. Actually I would probably externalize it in a custom action filter to avoid repeating it in every controller action that needs to be invoked from a cross domain AJAX call. There are no additional steps necessary. Just make sure that your browser supports CORS because if it doesn't adding this line will have strictly no effect whatsoever.

HTTP request is not breaking up the path from querystring in some requests

I have a weird problem which don't know how to solve.
The usual behavior for .NET is if you have a URL like: /action?id=abc&=type=3
the server variable are like:
URL: /action
PATH_INFO: /action
QUERY_STRING: id=abc&=type=3
But here comes the weird behavior: when the request comes from mediapartners-google, the server variables don't split the URL at the question mark and are:
URL: /action?id=abc&=type=3
PATH_INFO: /action?id=abc&=type=3
QUERY_STRING:
So the routing engine is trying to find an action name called action?id=abc&=type=3 and it doesn't find it, providing a 404 error.
Does anyone knows why this happens?
For MVC when the routing engine has matched a route the rest gets stuffed into the optional parameter. It quite possible to have a route match before it reaches the question mark. Its looks like you need another route to handle this case. Also remember you need your routes in order of strongest to weakest as they are processed by the first route that matches.

Mapping querystrings in routing

A site uses this link format for everything:
example.com/?article=123
example.com/?category=456
example.com/?article=789&picture=012
An ASP classic default.asp catches all of this and does stuff with it.
I would really like to create a couple of routes for these old type of Url's with querystrings on the root, so I can catch them in a separate controller and do some tricks on them (permanent redirects).
When I try to create a route that contains a questionmark, I am told that is not possible.
This could be a good case for Global Action Filters if there's no way to handle these using routes (Although I imagine there is).
You could process the incoming url in the OnActionExecuting method and redirect as appropriate.
UPDATE
There's a good SO answer here on how to redirect as you require. It may not be exactly as you've asked however a similar principle should acheive what you're after quite easily.

What's the trouble with my ASP.NET MVC route?

I have some routes in my ASP.NET MVC application that handle redirecting of old urls. The URL I'm redirecting is:
contentSpanishContentList.aspx
Here's the route:
routes.MapRoute("RedirectLegacyContent1",
"content{contentUri}.aspx",
new { controller = "Redirect", action = "Content", contentUri = string.Empty, contentId = 0 });
The problem is it comes up as not found. I figured out that the problem is (in bold) contentSpanish*Content*List.aspx. What should I do to make this route work with this case?
Two solutions
Rename your pages to not include the same constant string (in your case it's the word content).
Write a custom route that's able to parse your requests - all you'll have to override is the GetRouteData method. And if you're planning to only use this route for incoming requests (not generating any URLs in your views to point to any of these pages ie. using Url.Action or Html.ActionLink) then the easiest way would be to generate something like a RegExRoute which would be easy to write and also easy to resolve these kind of requests.
The fist one is simple, the second is universal.
Use Fiddler to look at what's happening. Is the 404 happening on the first request? Or is it happening after the redirect?
Install the RouteDebugger package and see what it tells you.

Resources