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

查看:18
本文介绍了如何从 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天全站免登陆