MVC路由问题 [英] MVC Routing Problem

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

问题描述

大家好,

我目前正在使用自己的ViewEngine开发一个MVC4项目,我遇到了一些路由问题。

I使用几种路由格式,似乎我的View没有收到与格式对应的路由值。我相信这是因为它无法抑制许多路线值,但我不知道如何在没有"弄脏"的情况下解决这个问题。我的代码
与另一个循环(例如:对于我的网站的每个部分,为每种可用语言创建路由。)

I am currently develloping an MVC4 project, with my own ViewEngine, and I am experiencing some routing problems.
I use a couple of route formats and it seems my View does not recieves the route values corresponding to the formats. I believe it is because it cannot restrain the many route values, but I do not know how to solve that without "dirtying" my code with another loop (ex: for every section of my website, create a route for every available language.)

以下URL正在运行:mywebsite.com,mywebsite .com / zh,  mywebsite.com/en/mysection

The following URL are working: mywebsite.com, mywebsite.com/en, mywebsite.com/en/mysection

但是,mywebsite.com / mysection应该可以工作,并使用我的MapRouteWithName方法中指定的默认值,但它不会吨。

However, mywebsite.com/mysection should work, and use the default value specified in my MapRouteWithName method, but it doesn't.

我相信这是因为我没有限制默认路线的语言价值,因此"mysection"也是如此。值被视为可能的语言短名称。



此外,我有一条路线(*) (使用以下网址映射:"{language} / mysectionname / {params}")只有在我没有指定params时才有效(例如:mywebsite.com/en/mysectionname")但不包含params(例如:mywebsite.com/en/mysectionname/0
> 404 Not Found)。
$
当我说它有效时,它实际上会触发一个异常,说"参数字典包含参数(..)的空条目"。 。

再次,我相信这是因为我将参数的类型转换为可以为空的类型时路由使用不当,它会停止抛出此异常,但不会解决实际问题。

(*)此路由指向"AdminInterfaceController"。使用"GenerateSectionDetails"行动。

I believe this happens because I did not restrain the default route's language value, and therefore the "mysection" value is considered as a possible language shortname.

In addition, I have a route (*) (mapped with the following URL: "{language}/mysectionname/{params}") that only works when I do not specify the params (ex: mywebsite.com/en/mysectionname") but does not with params (ex: mywebsite.com/en/mysectionname/0 > 404 Not Found).
When I say it works, it actually fires an exception, saying that "The parameters dictionary contains a null entry for parameter (..)".
Again, I believe this happens because of my bad use of routes for when I convert my parameter's type to a nullable type, it stops throwing this exception, but does not solve the real problem.
(*) this route points to the "AdminInterfaceController" with the "GenerateSectionDetails" action.

我希望我能够解释我的问题,并且以下来源会有所帮助。


     - 第一个片段:我相信它将有助于解决我对路线的错误使用和/或添加足够的RouteRestrictions。

     - 第二个片段:可能有助于解决我的"(...)包含空条目(..)"问题。

I hope I was able to explain my problem, and that the following sources will help.
     - The first snippet: I believe it will help in the solving of my bad use of routes and/or to add the adequate RouteRestrictions.
     - The second snippet: will possibly help in the solving of my "(...)contains a null entry(..)" problem.

------------------------------------- ----------------------
$
我的"RegisterRoutes"方法:

---------------------------------------- -------------------

-----------------------------------------------------------
My "RegisterRoutes" method:
-----------------------------------------------------------

     Dim defaultLanguage As Localization.Language = iWebsiteInfrastructure.GetDefaultLanguage

           如果defaultLanguage Nothing则为
                '抛出异常

               退出子

           结束如果

            Dim defaultSection As AxonWebsiteSection = iWebsiteInfrastructure.GetDefaultSection

           如果defaultSection为Nothing则为
                Dim activeSections As Collections.AxonWebsiteSectionCollection = iWebsiteInfrastructure.Sections.GetActiveWebsiteSections

               如果activeSections.Count> 0然后

                    defaultSection = activeSections.First

               结束如果

           结束如果

            Dim routeName As String = Nothing

            Dim controllerName As String = Nothing

            Dim actionName As String = Nothing

            Dim url As String = Nothing

            Dim defaultParam As Object = Nothing

           对于每个部分在iWebsiteInfrastructure.Sections.GetActiveWebsiteSections

                routeName = section.RouteName

               如果routeName =没有那么

                  &NBSP ; routeName = section.Name

               结束如果

                controllerName = section.ControllerConfiguration.ControllerName

               如果controllerName =没有那么

                  &NBSP ; controllerName =" FrameworkDefault"

               否则,
                    section.ControllerConfiguration.LoadController()

               结束如果

                actionName = section.ControllerConfiguration.ControllerDefaultAction

               如果actionName =没有那么

                  &NBSP ; actionName =" GenerateDefaultContents"

               结束如果

                url =" {language}"

                url + =" /"

                url + = routeName

                url + =" /"

                url + =" {action}"

               如果section.ControllerConfiguration.ControllerActionParameters.Count> 0然后

                    URL + = QUOT; / {PARAMS}"

               &NBSP ;    defaultParam = New With {.language = defaultLanguage.ShortName,.controller = controllerName,.action = actionName,.params = UrlParameter.Optional}

        ;        否则,
                    defaultParam = New With {.language = defaultLanguage.ShortName,.controller = controllerName,.action = actionName}

                结束如果

                RouteTable.Routes.MapRouteWithName(section.Name,url,defaultParam)

           下一个

            routeName = defaultSection.RouteName

            url =" {language}"

            url + =" /"

            url + =" {controller}"

            url + =" /"

            url + =" {action}"

            controllerName = defaultSection.ControllerConfiguration.ControllerName

           如果controllerName = Nothing则为
                controllerName =" FrameworkDefault"

           否则b $ b                defaultSection.ControllerConfiguration.LoadController()

           结束如果

            actionName = defaultSection.ControllerConfiguration.ControllerDefaultAction

           如果actionName = Nothing则为
                actionName =" GenerateDefaultContents"

           结束如果

            defaultParam =新借助{。外语= defaultLanguage.ShortName,.controller = controllerName,.action = actionName}

             RouteTable.Routes.MapRouteWithName(routeName,url,defaultParam)

    Dim defaultLanguage As Localization.Language = iWebsiteInfrastructure.GetDefaultLanguage
            If defaultLanguage Is Nothing Then
                'Throw exception
                Exit Sub
            End If
            Dim defaultSection As AxonWebsiteSection = iWebsiteInfrastructure.GetDefaultSection
            If defaultSection Is Nothing Then
                Dim activeSections As Collections.AxonWebsiteSectionCollection = iWebsiteInfrastructure.Sections.GetActiveWebsiteSections
                If activeSections.Count > 0 Then
                    defaultSection = activeSections.First
                End If
            End If
            Dim routeName As String = Nothing
            Dim controllerName As String = Nothing
            Dim actionName As String = Nothing
            Dim url As String = Nothing
            Dim defaultParam As Object = Nothing
            For Each section In iWebsiteInfrastructure.Sections.GetActiveWebsiteSections
                routeName = section.RouteName
                If routeName = Nothing Then
                    routeName = section.Name
                End If
                controllerName = section.ControllerConfiguration.ControllerName
                If controllerName = Nothing Then
                    controllerName = "FrameworkDefault"
                Else
                    section.ControllerConfiguration.LoadController()
                End If
                actionName = section.ControllerConfiguration.ControllerDefaultAction
                If actionName = Nothing Then
                    actionName = "GenerateDefaultContents"
                End If
                url = "{language}"
                url += "/"
                url += routeName
                url += "/"
                url += "{action}"
                If section.ControllerConfiguration.ControllerActionParameters.Count > 0 Then
                    url += "/{params}"
                    defaultParam = New With {.language = defaultLanguage.ShortName, .controller = controllerName, .action = actionName, .params = UrlParameter.Optional}
                Else
                    defaultParam = New With {.language = defaultLanguage.ShortName, .controller = controllerName, .action = actionName}
                End If
                RouteTable.Routes.MapRouteWithName(section.Name, url, defaultParam)
            Next
            routeName = defaultSection.RouteName
            url = "{language}"
            url += "/"
            url += "{controller}"
            url += "/"
            url += "{action}"
            controllerName = defaultSection.ControllerConfiguration.ControllerName
            If controllerName = Nothing Then
                controllerName = "FrameworkDefault"
            Else
                defaultSection.ControllerConfiguration.LoadController()
            End If
            actionName = defaultSection.ControllerConfiguration.ControllerDefaultAction
            If actionName = Nothing Then
                actionName = "GenerateDefaultContents"
            End If
            defaultParam = New With {.language = defaultLanguage.ShortName, .controller = controllerName, .action = actionName}
            RouteTable.Routes.MapRouteWithName(routeName, url, defaultParam)

--------------------------- -------------------------------------------------- ------------
$
My" AdminInterfaceController.GenerateSectionDetails"方法

----------------------------------------- ------------------------------------------------

-----------------------------------------------------------------------------------------
My "AdminInterfaceController.GenerateSectionDetails" method
-----------------------------------------------------------------------------------------

   公共函数GenerateSectionDetails(ByVal index As Integer)As ActionResult

    Public Function GenerateSectionDetails(ByVal index As Integer) As ActionResult

       如果不是索引> = 0或不索引< Axon.AxonEngine.WebsiteInfrastructure.Sections.Count然后是
            '抛出异常

           返回新的HttpNotFoundResult

           退出功能

       结束如果

        Dim section As Axon.AxonWebsiteSection = Axon.AxonEngine.WebsiteInfrastructure.Sections(index)

       如果部分为Nothing那么为
            '抛出异常

           返回新的HttpNotFoundResult

           退出功能

       结束如果

        ViewData.Add(" masterpages",Axon.AxonEngine.WebsiteInfrastructure.MasterPages)

        ViewData.Add(" section",section)

        ViewData.Add(" sections",Axon.AxonEngine.WebsiteInfrastructure.Sections)

       返回视图(新Axon.AxonView(Me.ControllerContext))
$
       退出功能

        If Not index >= 0 Or Not index < Axon.AxonEngine.WebsiteInfrastructure.Sections.Count Then
            'Throw exception
            Return New HttpNotFoundResult
            Exit Function
        End If
        Dim section As Axon.AxonWebsiteSection = Axon.AxonEngine.WebsiteInfrastructure.Sections(index)
        If section Is Nothing Then
            'Throw exception
            Return New HttpNotFoundResult
            Exit Function
        End If
        ViewData.Add("masterpages", Axon.AxonEngine.WebsiteInfrastructure.MasterPages)
        ViewData.Add("section", section)
        ViewData.Add("sections", Axon.AxonEngine.WebsiteInfrastructure.Sections)
        Return View(New Axon.AxonView(Me.ControllerContext))
        Exit Function

   结束函数

    End Function

推荐答案

也许您可能想在此链接上尝试论坛
http://www.asp.net/mvc/mvc4
Perhaps you may want to try forums at this link http://www.asp.net/mvc/mvc4


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

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