具有多个控制器的不同区域的MVC路由 [英] MVC Routing with different Areas with multiple controllers

查看:115
本文介绍了具有多个控制器的不同区域的MVC路由的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的解决方案结构如下:

Areas
    - Games
        -Controllers
        -Views etc
    - Movies
        -Controllers
            - MoviesController.cs
            - MovieCalendarController.cs
            - MovieSearchController.cs
        -Views etc

现在我希望能够做到这一点: 导航到 https://localhost/Movies/并点击MoviesController.cs

的索引

导航至: https://localhost/Movies/Calendar/并点击MovieCalendarController.cs

的索引

最后导航到 https://localhost/Movies/Search/并点击MovieSearchController.cs

我尝试过但无法正常工作(出现No route in the route table matches the supplied values.)错误:

MovieAreaRegistration.cs

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

            context.MapRoute(
                "Calendar_default",
                "Movies/Calendar/",
                new { controller = "MovieCalendar", action = "Index", id = UrlParameter.Optional }
            );


            context.MapRoute(
                "Search_default",
                 "Movies/Search/{action}/{id}",
                    new { controller = "MovieSearch", action = "Index", id = UrlParameter.Optional }
            );
        }

抱歉,我是区域和路线的新手

更新

使用属性路由后,我陷入了这个问题:

找到与URL匹配的多种控制器类型.这个可以 如果多个控制器上的属性路由与请求的匹配,则会发生 网址.

该请求已找到以下匹配的控制器类型: MovieCalendar.UI.Areas.Movies.Controllers.MovieCalendarController MovieCalendar.UI.Areas.Movies.Controllers.MoviesController

电影控制器

[RouteArea("Movies")]
[Route("{action}")]
public class MoviesController : BaseController
{
}

日历控制器

[RouteArea("Movies")]
[RoutePrefix("Calendar")]
[Route("{action=Index}")]
public class MovieCalendarController : BaseController
{
}

这种情况发生在访问URL http://localhost/Movies/Calendar 时,它希望我进入MovieCalendarController索引操作方法.我可以理解为什么会抱怨,因为MovieController中可能有一个名为Calendar的ActionMethod(没有).

解决方案

使用属性路由可能会更好.它将允许您执行以下操作:

   public class MoviesController : Controller {

        [Route("Movies")]
        public ActionResult Index() {
            return this.View();
        }
    }

    public class MovieCalendarController : Controller {

        [Route("Movies/Calendar")]
        public ActionResult Index() {
            return this.View();
        }
    }

然后您可以摆脱当前的 route 映射,并使用它来初始化您的路由:

RouteTable.Routes.MapMvcAttributeRoutes();

有关属性路由的更多信息,请参见

此路由将匹配以 Movies/开头的URL,后跟任何字符串,包括 Calendar .因此,此路线将与

冲突

[RouteArea("Movies")]
[RoutePrefix("Calendar")]
[Route("{action=Index}")]
public class MovieCalendarController : BaseController
{
}

使用用于控制器的命名约定,基于公约的路由将很困难.

My solution structure is the following:

Areas
    - Games
        -Controllers
        -Views etc
    - Movies
        -Controllers
            - MoviesController.cs
            - MovieCalendarController.cs
            - MovieSearchController.cs
        -Views etc

Now what I would like is to be able to do this: Navigate to https://localhost/Movies/ and hit the index of the MoviesController.cs

Navigate to: https://localhost/Movies/Calendar/ and hit the index of the MovieCalendarController.cs

And lastly navigate to https://localhost/Movies/Search/ and hit the index of the MovieSearchController.cs

What I have tried but is not working (getting No route in the route table matches the supplied values.) errors:

MovieAreaRegistration.cs

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

            context.MapRoute(
                "Calendar_default",
                "Movies/Calendar/",
                new { controller = "MovieCalendar", action = "Index", id = UrlParameter.Optional }
            );


            context.MapRoute(
                "Search_default",
                 "Movies/Search/{action}/{id}",
                    new { controller = "MovieSearch", action = "Index", id = UrlParameter.Optional }
            );
        }

Apologies, I'm new to areas and routing

Update

After using attribute routing I have fell into this problem:

Multiple controller types were found that match the URL. This can happen if attribute routes on multiple controllers match the requested URL.

The request has found the following matching controller types: MovieCalendar.UI.Areas.Movies.Controllers.MovieCalendarController MovieCalendar.UI.Areas.Movies.Controllers.MoviesController

Movies Controller

[RouteArea("Movies")]
[Route("{action}")]
public class MoviesController : BaseController
{
}

Calendar Controller

[RouteArea("Movies")]
[RoutePrefix("Calendar")]
[Route("{action=Index}")]
public class MovieCalendarController : BaseController
{
}

This happens when accessing the url http://localhost/Movies/Calendar hoping it will take me to the MovieCalendarController Index action method. I can see why it's complaining because there could be a ActionMethod in the MovieController called Calendar (There is not).

解决方案

You may be better off with Attribute Routing. It will let you do this:

   public class MoviesController : Controller {

        [Route("Movies")]
        public ActionResult Index() {
            return this.View();
        }
    }

    public class MovieCalendarController : Controller {

        [Route("Movies/Calendar")]
        public ActionResult Index() {
            return this.View();
        }
    }

And then you can get rid of your current route mappings and use this initialize your routes:

RouteTable.Routes.MapMvcAttributeRoutes();

More information on attribute routing can be found here.

Update

[RouteArea("Movies")]
[Route("{action}")]
public class MoviesController : BaseController
{
}

This route will match urls starting with Movies/ followed by any string, including Calendar. So this route will clash with:

[RouteArea("Movies")]
[RoutePrefix("Calendar")]
[Route("{action=Index}")]
public class MovieCalendarController : BaseController
{
}

Convention based routing will be difficult with the naming convention your are using for your controllers.

这篇关于具有多个控制器的不同区域的MVC路由的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆