如何替换“-”与“_”在 .net mvc3 应用程序中映射控制器和操作之前的 url 中

发布于 2024-12-09 08:53:10 字数 454 浏览 0 评论 0原文

大家好:我正在开发 .net mvc3 应用程序。 这是我的控制器和操作:

public class Foo_Bar : Controller
{
     public ViewResult Foo_Bar()        
    {

        return View();
    }
}

我的对应视图文件是:Foo_Bar.cshtml

现在,我的问题是:我的网址为:www.mystite.com/foo-bar/foo-bar 但此网址无法调用 Foo_Bar/Foo_Bar 控制器和操作,除非我将其写为: www.mystite.com/foo_bar/foo_bar 。

现在我需要在映射我的控制器和操作的路线系统之前将“-”替换为“_”。我可以在routes.MapRoute()中做到这一点吗?有人请帮助我。先谢谢你了!ps:这是我第一次在堆栈溢出中提出问题:)

Hello everyone :I am working on a .net mvc3 application.
Here is my controller and action :

public class Foo_Bar : Controller
{
     public ViewResult Foo_Bar()        
    {

        return View();
    }
}

And my corespondent view file is : Foo_Bar.cshtml

now ,my question is : I have a url as: www.mystite.com/foo-bar/foo-bar
but this url can not invoke the Foo_Bar/Foo_Bar controller and action except I write it as thes: www.mystite.com/foo_bar/foo_bar .

Now I need to replace the "-" with "_" before the route system mapping my controller and action. Can I do it in the routes.MapRoute()? Some one please help me .Thank you advance !ps:this is my first time to ask an question in Stack overflow:)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

挖鼻大婶 2024-12-16 08:53:10

您可以创建自定义 RouteHandler 来更改 {action}{controller} 的值。首先,创建 RouteHandler,如下所示:

using System.Web.Mvc;  
using System.Web.Routing;  

public class MyRouteHandler : IRouteHandler  
{  
    public IHttpHandler GetHttpHandler(RequestContext requestContext)  
    {  
        var routeData = requestContext.RouteData;  
        var controller = routeData.Values["controller"].ToString(); 
        routeData.Values["controller"] = controller.Replace("-", "_");  
        var action = routeData.Values["action"].ToString();  
        routeData.Values["action"] = action.Replace("-", "_");  
        var handler = new MvcHandler(requestContext);  
        return handler;  
    }  
}  

然后,按如下方式更改默认路由:

routes.Add("Default",  
    new Route("{controller}/{action}/{id}",  
        new RouteValueDictionary(  
            new { controller = "Home", action = "Index", id = UrlParameter.Optional }),  
            new MyRouteHandler() 
        ) 
    );  
); 

当此路由满足请求时,RouteHandler 会将破折号更改为下划线。

You can create a custom RouteHandler to change the values for {action} and {controller}. First, create your RouteHandler, as follows:

using System.Web.Mvc;  
using System.Web.Routing;  

public class MyRouteHandler : IRouteHandler  
{  
    public IHttpHandler GetHttpHandler(RequestContext requestContext)  
    {  
        var routeData = requestContext.RouteData;  
        var controller = routeData.Values["controller"].ToString(); 
        routeData.Values["controller"] = controller.Replace("-", "_");  
        var action = routeData.Values["action"].ToString();  
        routeData.Values["action"] = action.Replace("-", "_");  
        var handler = new MvcHandler(requestContext);  
        return handler;  
    }  
}  

Then, change your Default route as follows:

routes.Add("Default",  
    new Route("{controller}/{action}/{id}",  
        new RouteValueDictionary(  
            new { controller = "Home", action = "Index", id = UrlParameter.Optional }),  
            new MyRouteHandler() 
        ) 
    );  
); 

When a request is satisfied by this route, the RouteHandler will change the dashes to underbars.

弃爱 2024-12-16 08:53:10

使用属性 [ActionName]

public class Foo_Bar : Controller
{
     [ActionName("foo-bar")]
     public ViewResult Foo_Bar()        
    {

        return View();
    }
}

控制器没有等效属性,因此您不能在带有 {controller} 的路由中使用它。但你至少可以明确地定义它。

Use the attribute [ActionName]

public class Foo_Bar : Controller
{
     [ActionName("foo-bar")]
     public ViewResult Foo_Bar()        
    {

        return View();
    }
}

There isn't an equivalent for Controllers, so you can't use it in your route with {controller}. But you can define it explicitly at least.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文