2016-07-07 110 views
1

http://blog.falafel.com/duplicate-controller-names-aspnet-mvc-areas/MVC4區域,主控制器和路由

按照此建議,我現在正在運行。但是,當擊中/ 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's Index.cshtml
etc ...

我嘗試過不同區域的屬性路由。

[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

回答

0

我發現,你可以到路由屬性添加一個空字符串路由名稱。

[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); 
    } 
    ... 
}