在ASP.NET MVC中路由之前,请替换url中的字符 [英] Replace character in url before routing in ASP.NET MVC

查看:68
本文介绍了在ASP.NET MVC中路由之前,请替换url中的字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以在路由url之前,即在MVC通过我的路由配置以查找要使用的路由之前操纵该网址.

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

我想将网址中的某些字符替换,例如将www.test.com/ä/ö"替换为"www.test.com/a/o".这样,如果用户在url中键入这些字母,则仍将使用正确的路由.

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?

为了阐明我想要的内容,我将添加一个示例.假设我有一个路由配置,如下所示:"{controller}/{action}".用户键入www.test.com/MyCöntroller/MyÄction,我希望将其路由到控制器"MyController"和操作方法"MyAction".在路由完成之前,我必须进行字符替换,否则将找不到匹配的路由.因此,我想在路由完成之前,将所有ö"替换为"o",并将所有ä"替换为"a"(以及更多字符).有什么办法吗?

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:经过一些研究,似乎是UrlRoutingModule首次在ASP.NET MVC中获得了url.也许有某种方法可以吸引到那?

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?

推荐答案

在此

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);
     }
 }

}

这篇关于在ASP.NET MVC中路由之前,请替换url中的字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆