【问题标题】:ASP MVC Area with RoutingConfig带有 RoutingConfig 的 ASP MVC 区域
【发布时间】:2025-12-31 20:05:03
【问题描述】:

我将在我的 ASP.Net MVC 项目中使用 Area,但我不确定我可以做我想做的事吗?因为我是 MVC 的新手。
所以我的数据库中有这样的名称:
名称1
名称2
名称5等

所以我想使用 area 并有一个这样的网址:
www.mysite.com/Name1/Home/Index

用户可以更改 Name1 ,如果 Name1 存在,我如何检查数据库?我的意思是我在哪里可以检查这个?

我的 AreaRegistration 喜欢这样:

public override void RegisterArea(AreaRegistrationContext context) 
{
    context.MapRoute(
        "MyArea_default",
        "{arename}/{controller}/{action}/{id}",
        new {action = "Index", id = UrlParameter.Optional }
    );
}

【问题讨论】:

    标签: asp.net-mvc asp.net-mvc-5 asp.net-mvc-areas


    【解决方案1】:

    为了实现所需的功能,您必须创建自定义路由约束。这在http://www.asp.net/mvc/tutorials/controllers-and-routing/creating-a-custom-route-constraint-cs 有深入解释:)

    您将在 Match() 方法中对现有数据库记录执行检查。请参阅提供的文档以获得进一步的解释:)它很容易阅读。

    【讨论】:

    • 我按照说明进行操作,但 IRouteConstraint 不会为我触发,我应该在哪里使用它来处理 AreaNames ?