Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 结合类似的ASP.NET MVC路由_C#_Asp.net Mvc - Fatal编程技术网

C# 结合类似的ASP.NET MVC路由

C# 结合类似的ASP.NET MVC路由,c#,asp.net-mvc,C#,Asp.net Mvc,我有两个非常相似的ASP.NET MVC路径,如下所示 是否需要组合这两个路由并放置一个条件语句,以便(1)对“logs/email/”的任何请求都转到EmailLogsController,而对“logs/user/”的请求转到UserLogsController // URL: /areax/logs/email/ // URL: /areax/logs/email/details/51 // URL: /areax/logs/email/details/117 context.MapRou

我有两个非常相似的ASP.NET MVC路径,如下所示

是否需要组合这两个路由并放置一个条件语句,以便(1)对“logs/email/”的任何请求都转到EmailLogsController,而对“logs/user/”的请求转到UserLogsController

// URL: /areax/logs/email/
// URL: /areax/logs/email/details/51
// URL: /areax/logs/email/details/117
context.MapRouteLowercase(
    "AreaX.Log.Email",
    "areax/logs/email/{action}/{id}",
    new
    {
        controller = "EmailLogs",
        action = "Index",
        id = UrlParameter.Optional
    },
    new[] { ControllerNamespaces.AreaX }
);

// URL: /areax/logs/user/
// URL: /areax/logs/user/details/1
// URL: /areax/logs/user/details/10
context.MapRouteLowercase(
    "AreaX.Log.User",
    "areax/logs/user/{action}/{id}",
    new
    {
        controller = "UserLogs",
        action = "Index",
        id = UrlParameter.Optional
    },
    new[] { ControllerNamespaces.AreaX }
);

就目前情况而言,它们看起来不太干。

如果可以重命名控制器(删除日志后缀),可以尝试使用{controller}关键字:

    context.MapRouteLowercase(
    "AreaX.Log.Email",
    "areax/logs/{controller}/{action}/{id}",
    new
    {
        action = "Index",
        id = UrlParameter.Optional
    },
    new[] { ControllerNamespaces.AreaX }
);

塞里。。不编译。。。给我一个minuteSee,这是我最初的解决方案,但要求像这样命名URL:/areax/logs/user/details/10而不是:/areax/logs/userlogs/details/10(注意重复的“logs”)意味着这个答案不太正确。谢谢你的建议。@Bern按照Stefano的建议,删除“Logs”后缀,你应该会没事的。我感谢你的努力,但你的建议是我更改URL命名要求以适应建议的解决方案。如果现在有办法使用MVC路由技术来满足需求,那么这就是我愿意接受的答案。即使我删除了引起其他问题的“Logs”后缀,即“UserLogsController”变成了“UserController”,这实际上是不正确的。请参阅stackoverflow.com/questions/3303801/…–Stefano Altieri刚刚编辑谢谢@StefanoAltieri-鉴于问题的性质,我希望有一个更简单的解决方案。我更新了我的答案。。。看看它是否对你有用