【问题标题】:MVC4 Areas, HomeControllers and RoutingMVC4 区域、HomeControllers 和路由
【发布时间】:2016-11-10 08:51:01
【问题描述】:

http://blog.falafel.com/duplicate-controller-names-aspnet-mvc-areas/

听从了这个建议,现在正在跑步。但是,当点击 /area/home/index 时,会触发正确的控制器操作,但它使用的是根站点中的 Index.cshtml,而不是 Area 文件夹中的 Index.cshtml!任何想法如何“解决”这个问题?

或者有没有更好的方法来处理路由来完成同样的事情?

目标: http://example.net/ -> HomeController / 索引(根)
http://example.net/area1 -> area1.HomeController.Index / area1 的 Index.cshtml
http://example.net/area1/NotIndex -> area1.HomeController.NotIndex / area1 的 NotIndex.cshtml
http://example.net/area2 -> area2.HomeController.Index / area2的Index.cshtml
等等……

我尝试了不同区域的属性路由。

[RouteArea("ProductReuse")]
[Route("ProductReuse")]
public partial class ProductReuseController : BaseProductReuseController
{
    [HttpGet]
    [Route("Index")]
    public ActionResult Index()
    {
        if (SessionUserLogin == null) return LoginActionResult(SessionAppName);

        var serviceResponse = _productReuseService.IndexQuery(SessionUserId);
        var response = _mapper.Map<IndexViewModel>(serviceResponse);

        return View(response);
    }
    ...
}

这很接近,但您仍然必须有一个如下所示的 URL: http://example.net/ProductReuse/Index

如果你尝试点击http://example.net/ProductReuse,它就会爆炸。

TIA

【问题讨论】:

    标签: c# asp.net-mvc-4 routing


    【解决方案1】:

    我发现你可以在路由属性中添加一个空字符串路由名称。

    [RouteArea("ProductReuse")]
    [Route("ProductReuse")]
    public partial class ProductReuseController : BaseProductReuseController
    {
        [HttpGet]
        [Route("Index")]
        [Route("")]  // Use an empty name to set the default page...
        public ActionResult Index()
        {
            if (SessionUserLogin == null) return LoginActionResult(SessionAppName);
    
            var serviceResponse = _productReuseService.IndexQuery(SessionUserId);
            var response = _mapper.Map<IndexViewModel>(serviceResponse);
    
            return View(response);
        }
        ...
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-10-03
      • 2013-06-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-03-17
      • 2010-12-09
      • 2012-02-19
      相关资源
      最近更新 更多