c# - ASP MVC 5 在 URL 中隐藏区域(路由)

标签 c# asp.net asp.net-mvc routes asp.net-mvc-5

我有三个区域:tpl1 - tpl2 - tpl3

当我启动应用程序时,我选择要使用的区域:

routes.MapRoute(
        "Default",
        "{controller}/{action}",
        new { controller = "Home", action = "Index" },
        new[] { "LojaOnline.Controllers" }
        ).DataTokens.Add("area", "tpl1");

(DataTokens.Add 中的“tpl1”是动态的。)

网址是:http://localhost/tpl1/Home/Index

我需要在 URL 中隐藏区域名称。类似的东西:

网址是:http://localhost/Home/Index

我在 tpl1AreaRegistration.cs 中有这个

context.MapRoute(
            "tpl1_default",
            "tpl1/{controller}/{action}/{id}",
            new {controller="Home", action = "Index", id = UrlParameter.Optional }
            );

但如果我删除 tpl1,应用程序将不知道使用哪个 Controller 。

context.MapRoute(
            "tpl1_default",
            "{controller}/{action}/{id}",
            new {controller="Home", action = "Index", id = UrlParameter.Optional }
            );

最佳答案

据我所知,在 IIS 中这是不可能的。

当您将 URL 设为 http://localhost/Home/Index 时。 首先,它将连接到您的本地 IIS,并在其中搜索“Home”这样的应用程序名称,并在其中根据您选择的 Web 应用程序搜索脚本页面或网络表单。但实际上根据您的 URL,您已将应用程序指定为 tpl1

所以你不能使用 http://localhost/Home/Index 而不是 http://localhost/tpl1/Home/Index

但是是的,您可以在末尾的 URL 中隐藏 Index,例如 http://localhost/tpl1/Home。 使用路由文件,您只能控制应用程序内部的 URL。

关于c# - ASP MVC 5 在 URL 中隐藏区域(路由),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30396306/

相关文章:

c# - 400 BAD 请求尝试联系 Azure 异常检测 API

c# - ListBox 使用 caliburn.micro 自动调整大小不正确

c# - 在 C# 中,为什么我不能在 foreach 循环中修改值类型实例的成员?

asp.net - 如何在 ASP .NET 中加粗文本

c# - .GetInt32() 和 Convert.ToInt32() 之间的区别

c# - 模型验证在单元测试中不起作用

c# - DispatcherTimer 堆栈 - UWP

asp.net-mvc - 在 MVC 中使用 ELMAH 时我需要 Try Catch 吗?为什么?

asp.net-mvc - 如何防止 View 将其模型传递给部分 View ,而是传递 null?

c# - 基于 HttpModule 的安全性是否足以保证 asp.net 应用程序的安全性?