【问题标题】:how to do routing in mvc , how to hide page extension name in mvc?如何在 mvc 中进行路由,如何在 mvc 中隐藏页面扩展名?
【发布时间】:2016-12-06 10:03:19
【问题描述】:

我有一个网站,我有一个名为 test.html 的页面。当我浏览http://www.domainName.com/test 时,它应该选择 test.html 页面而不使用控制器和操作方法。

我要在 routeconfig.cs 中写什么

 routes.MapRoute(
                name: "",
                url: "",
                defaults: new
                {

                });

【问题讨论】:

    标签: asp.net-mvc asp.net-mvc-4 url-routing global-asax


    【解决方案1】:

    你可以通过两种方式实现它

    在您的路线配置中:

    routes.MapPageRoute("Yourhtmlroute","Yourcustomurl","Path/To/Your/test.html");
    

    在您的控制器中:

    return Redirect("YourCustomUrl");
    

    【讨论】:

    • 如果 "Path/To/Your/test.html" 是 html 文件路径,那么 "Yourhtmlroute" 是什么?
    • localhost/smartpond/MyPage.html(working),但 localhost/smartpond/MyPage 不工作 routes.MapPageRoute("MyPage.html", "MyPage", "~/MyPage.html");
    • error-VirtualPath 必须是一个以 ~/ 开头的非空字符串。参数名称:virtualPath
    • 只需添加 return Redirect("MyPage.html");在您的操作方法中并将您的 html 文件放在相应的视图文件夹中,不要对路由配置做任何事情
    猜你喜欢
    • 2014-02-09
    • 1970-01-01
    • 2015-07-18
    • 2016-10-11
    • 2013-01-11
    • 1970-01-01
    • 1970-01-01
    • 2012-03-01
    • 2017-09-24
    相关资源
    最近更新 更多