2016-07-07 3 views
1

http://blog.falafel.com/duplicate-controller-names-aspnet-mvc-areas/MVC4 Области, HomeControllers и маршрутизация

последовал этому совету и теперь работает. Однако при нажатии/area/home/index срабатывает правильное действие контроллера, но он использует Index.cshtml из корневого сайта, а не Index.cshtml из папки Area! Любые идеи, как это исправить?

Или есть лучший способ справиться с маршрутизацией, чтобы выполнить одно и то же?

Цель: http://example.net/ -> HomeController/Index (корень)
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
и т. д.

Я пробовал маршрутизацию атрибутов для другой области.

[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 оно бомбы вне.

ТИА

ответ

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); 
    } 
    ... 
} 
Смежные вопросы