如何从MVC项目中的URL中删除控制器名称 [英] How to remove controller name from URL in MVC project

查看:117
本文介绍了如何从MVC项目中的URL中删除控制器名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个如下所示的URL,我需要删除控制器名称(myController)。我使用了多个修复程序,但均未解决该问题。

I have a URL like below and I need to remove the controller name (myController). I've use several fixes but none fixed the issue. Please help me guys..

http://foldername.example.com/foldername/myController/my-page

example.com是域和所有相关文件该网站位于 foldername文件夹中。 my-page是视图名称

'example.com' is the domain and all the files related to the site is inside 'foldername' folder. 'my-page' is the view name

最后,我需要上面的URL如下所示。

In the end I need the above URL to be like below.

http://example.com/my-page

先谢谢大家.. !!

Thank in advance guys..!!

推荐答案

您可以尝试通过放置更多内容来反转路线定义专业路线优先。另外,您可能不想将操作名称硬编码为操作,而是使用 {action} 占位符:

You could try inverting the routes definition by placing the more specialized route first. Also you probably didn't want to hardcode the action name as action but rather use the {action} placeholder:

public class RouteConfig
{
    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
        //routes.MapMvcAttributeRoutes();

    routes.MapRoute(
        name: "Special",
        url: "{action}",
        defaults: new { controller = "Home", action = "LandingIndex" }
    );

    routes.MapRoute(
        name: "Default",
        url: "{controller}/{action}/{id}",
        defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
    );
   }
}

点击以下链接:

链接1

链接2

链接3

这篇关于如何从MVC项目中的URL中删除控制器名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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