0

Can I manipulate the url before routing it, i.e. before MVC goes through my route configuration to find the route to use.

I'd like to replace some characters in the url, for example "www.test.com/ä/ö" to "www.test.com/a/o". That way, if a user typed those letter in the url, the right route would still be used.

Maybe there´s something that I can hook into to manipulate the url?

Edit: To clarify what I want I'll add an example. Let's say I have a routing configuration that looks like this: "{controller}/{action}". The user types www.test.com/MyCöntroller/MyÄction and I want to route that to the controller "MyController" and the action method "MyAction". I have to do the character replacement before the routing is done, otherwise no matching route will be found. Thus I'd like to replace all "ö" with "o" and all "ä" with "a" (and some more characters) BEFORE the routing is done. Is there any way to do this?

Edit2: After some research it seems like it is UrlRoutingModule that is the first to get the url in ASP.NET MVC. Maybe there is some way to hook into that?

1

2 Answers 2

1

Take a loot at this post, by creating custom route handler it is possible.

using System.Web.Routing; 
namespace My.Services
{
    public class MyRouteHander : IRouteHandler
    {
     ApplicationDbContext Db = new ApplicationDbContext();
     public IHttpHandler GetHttpHandler(RequestContext requestContext)
     {
         // Get route data values
         var routeData = requestContext.RouteData;
         var action = routeData.GetRequiredString("action");
         var controller = routeData.GetRequiredString("controller");

         //modify your action name here

             requestContext.RouteData.Values["action"] = actionName;
             requestContext.RouteData.Values["controller"] = "SpecialController";

         return new MvcHandler(requestContext);
     }
 }

}

0

Check out the answer to this question.

Basically you'll want to use the FilterAttribute with IActionFilter, and then apply the annotation to the ActionResult that services the route. This way you have an intermediary method to manipulate the URL before it's processed by your route configuration.

1
  • I don't have much experience with action filters, but I don't think that will help me. If I understand them correctly, actionfilters will be executed before or after the action method itself is executed. What I want is to manipulate the url before a controller and action method is even selected (by the routing config).
    – haagel
    Commented Apr 25, 2013 at 6:50

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.