MVC3区域,路由和网址 [英] MVC3 areas, routing and urls

查看:69
本文介绍了MVC3区域,路由和网址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在过去9个月左右的时间里,我开始沉迷于MVC,而我似乎一直在苦苦挣扎的一个领域是区域和路由".

Having absorbed myself in MVC over the last 9 months or so, the one area I seem to be struggling with are Areas and Routing.

有人可以帮助的几个问题:

A couple of questions if anyone may help:

1.)MVC3应用程序及其关联区域可以具有不同的Urls吗?

1.) Can an MVC3 application and it's associated areas have different Urls?

示例:

  • 主应用程序(根)= www.mymvcapp.com
  • 移动应用(区域)= m.mymvcapp.com
  • 管理应用程序(区域)= admin.mymvcapp.com
  • CustomerService(区域)= custsvc.mymvcapp.com

等,等等,等等...

etc, etc, etc...

任何提示或解决方案都应事先得到赞赏.似乎很多关于MVC的书籍都只是笼罩在Areas和Routing上.

Any tips or solutions greatly appreciated in advance. It just seems that a lot of the books out there on MVC trend to just glaze over Areas and Routing in general.

同样,这是专门针对"AREAS"和路由的.

Again, this is pretty specific to "AREAS" and routing.

谢谢.

修改2012年1月26日:

第一件事是让实际区域工作.直到昨天深夜,我再也没有运气了.

First thing was to get the actual area to work. I've never had any luck with this until I dove in late yesterday.

我创建了一个测试MVC项目(MvsAreas),该项目具有一个"Home"控制器和一个单独的"Index"操作方法和视图,到目前为止效果很好. :)

I created a test MVC project (MvsAreas) with a "Home" controller and a single "Index" action method and view, so good so far. :)

添加了一个名为"Admin"的区域,添加了一个名为"HomeController"的控制器,并添加了一个"Index"操作方法和视图.

Added an area called "Admin", added a controller called "HomeController" and added a single "Index" action method and view.

由于我有两个家庭控制器,因此会出现运行时错误.解决方法:使用名称空间,然后使用重载的MapRoute方法传入名称空间.

Because I have two home controllers, you will get a run time error. Resolution: use namespaces and then use the overloaded MapRoute method to pass in the namespace.

根应用程序控制器:

Namespace MvcAreas.Web.Mvc.Controllers

    Public Class HomeController
        Inherits Controller

        Function Index() As ActionResult

            Return View()

        End Function

    End Class

End Namespace

管理区域控制器:

Namespace MvcAreas.Areas.Admin.Controllers

    Public Class HomeController
        Inherits Controller

        Function Index() As ActionResult

            Return View()

        End Function

    End Class

End Namespace

Global.asax

Namespace MvcAreas.Web.Mvc

    Public Class MvcApplication
        Inherits HttpApplication

        Shared Sub RegisterGlobalFilters(ByVal filters As GlobalFilterCollection)

            filters.Add(New HandleErrorAttribute())

        End Sub

        Shared Sub RegisterRoutes(ByVal routes As RouteCollection)

            routes.IgnoreRoute("{resource}.axd/{*pathInfo}")

            routes.MapRoute(
                "Root_Default",
                "{controller}/{action}/{id}",
                New With {.controller = "Home", .action = "Index", .id = UrlParameter.Optional},
                New String() {"MvcAreas.Web.Mvc.Controllers"}
            )

        End Sub

        Sub Application_Start()

            AreaRegistration.RegisterAllAreas()

            RegisterGlobalFilters(GlobalFilters.Filters)
            RegisterRoutes(RouteTable.Routes)

        End Sub

    End Class

End Namespace

AdminAreaRegistration.vb

Namespace MvcAreas.Areas.Admin

    Public Class AdminAreaRegistration
        Inherits AreaRegistration

        Public Overrides ReadOnly Property AreaName() As String
            Get
                Return "Admin"
            End Get
        End Property

        Public Overrides Sub RegisterArea(ByVal context As System.Web.Mvc.AreaRegistrationContext)

            context.MapRoute(
                "Admin_default",
                "Admin/{controller}/{action}/{id}",
                New With {.controller = "Home", .action = "Index", .id = UrlParameter.Optional},
                New String() {"MvcAreas.Areas.Admin.Controllers"}
            )

        End Sub

    End Class

End Namespace

这将使区域和根起作用.

That will get the area and the root working.

下一步;尝试使用下面的一些参考链接来设置Urls.

Next step; attempt to use some of the reference link below to set up Urls.

如果有人有任何建议,请随时添加.

If anyone has any suggestions, please feel free to add.

推荐答案

使用Lucero的链接(顺便说一句Lucero,这为我指明了正确的方向),我能够使它正常工作.

Using Lucero's link (thank you BTW Lucero, this pointed me in the right direction), I was able to get this to work.

在上述项目中,我添加了一个名为"HostnameConstraint"的新类.

Adding to the above project, I added a new class called "HostnameConstraint".

HomenameConstraint.vb

Namespace MvcAreas.Web.Mvc.Constraints

    Public Class HostnameConstraint
        Implements IRouteConstraint

        Protected Property Hostname As String

        Public Sub New(hostName As String)

            Me.Hostname = hostName

        End Sub

        Private Function Match(httpContext As HttpContextBase, route As Route, parameterName As String, values As RouteValueDictionary,
                               routeDirection As RouteDirection) As Boolean Implements IRouteConstraint.Match

            If httpContext.Request.Url.Host = Me.Hostname Then
                Return True
            End If

            Return False

        End Function

    End Class

End Namespace

在继续之前,您必须为要与站点关联的任何主机名设置IIS(我知道这很明显,但是有人会问). :)

Before I go on, you do have to set up IIS for whatever hostnames you want to be associated with the site (I know this is pretty obvious, but someone will ask). :)

现在更改Global.asax

        Shared Sub RegisterRoutes(ByVal routes As RouteCollection)

            routes.IgnoreRoute("{resource}.axd/{*pathInfo}")

            routes.MapRoute(
                "Test2",
                "{controller}/{action}",
                New With {.controller = "Home", .action = "Index"},
                New With {.hostname = New MvcAreas.Web.Mvc.Constraints.HostnameConstraint("www.mvcareas.com")},
                New String() {"MvcAreas.Web.Mvc.Controllers"}
                )

        End Sub

现在更改AdminAreaRegistration.vb

Public Overrides Sub RegisterArea(ByVal context As AreaRegistrationContext)

    context.MapRoute(
        "Admin",
        "{controller}/{action}",
        New With {.controller = "Home", .action = "Index"},
        New With {.hostname = New MvcAreas.Web.Mvc.Constraints.HostnameConstraint("admin.mvcareas.com")},
        New String() {"MvcAreas.Areas.Admin.Controllers"}
        )


End Sub

现在,当我运行该应用程序时,www.mvcareas.com会带我从根主目录控制器执行正确的Index操作.

Now when I run the application, www.mvcareas.com brings me to the correct Index action from the root home controller.

当我输入admin.mvcareas.com时,我会从管理区域主控制器进入正确的索引操作.

When I enter in admin.mvcareas.com, I am brought to the correct index action from the admin area home controller.

一切都很好,我是一个快乐的露营者. :)

All is well and I am a happy camper. :)

顺便说一句,我知道这是一个非常简单的区域,URL和路由概念的应用程序.但是希望这篇文章能帮助那些想更好地理解它或喜欢我的人,看看我们如何满足项目的某些特定URL要求.

BTW, I know this is a very simplistic application of areas, urls, and routing concepts. But hopefully this post helps someone out that is trying to understand it better or like me, see how we can meet some specific url requirements for a project.

Lucero,再次感谢您提供的链接.

Lucero, thanks again for the link.

这篇关于MVC3区域,路由和网址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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