Skip to main content
 首页 » 编程设计

c#之MVC 区域路由

2024年02月06日12over140

我有一个 MVC 应用程序如下:

Application 
 Controllers 
      HomeController 
           ActionResult Index(); 
 Areas 
      IT 
           Controllers 
                ITHomeController 
                     ActionResult Index(); 

我已设置链接以在主应用程序和 IT 区域之间正常工作。

在 URL 中,IT 区域的链接显示为 http://localhost/IT/ITHome . 链接的代码是:

@Html.ActionLink("IT Page","Index","ITHome", new { area = "IT" }, null) 

我如何让 url 简单地成为 http://localhost/IT我相信这很简单。

谢谢!

请您参考如下方法:

  1. 寻找/Areas/IT/ITAreasRegistration.cs
  2. 在其public override void RegisterArea(AreaRegistrationContext context)中添加路由映射方法,最好在最顶部或至少在该区域的默认路由之前。

类似于:

context.MapRoute( 
                "default_IT", 
                "IT", 
                new { action = "Index", controller = "ITHome" }, 
                new[] { "YourAppNamespaceHere.Areas.IT.Controllers" } 
            ); 
  1. 至于链接...我只是将其编码为<a href="/IT">IT Home</a>并完成它,除非您将其作为 wwwroot 的子应用程序托管,否则您应该没问题。

通过这种方式,您可以将区域路线与正常路线分开。