What is wrong with my area routing in my bin deployed MVC4 app?

后端 未结 5 1884
我在风中等你
我在风中等你 2021-01-13 04:36

I have just deployed an MVC4 .NET 4.0 app to my web host, for \'live\' deployed testing. Non -area routes are working fine, e.g. my

@Html.ActionLink(\"Regis         


        
相关标签:
5条回答
  • 2021-01-13 05:03

    Do you happen to inherit PortableAreaRegistration from MvcContrib in any of your areas?

    We used to have exactly the same symptoms (works locally, not on server) until we removed all the PortableAreaRegistrations from Portable Areas and reverted to using simply AreaRegistration since MVC4 can register a portable area without any additional libraries.

    0 讨论(0)
  • 2021-01-13 05:05

    In your global.asax.cs make sure you have this line in your Application_Start:

    AreaRegistration.RegisterAllAreas();
    

    In each area, you should have AreaRegistration.cs something like this:

    public class testAreaRegistration : AreaRegistration
    {
        public override string AreaName
        {
            get
            {
                return "test";
            }
        }
    
        public override void RegisterArea(AreaRegistrationContext context)
        {
            context.MapRoute(
                "test_default",
                "test/{controller}/{action}/{id}",
                new { action = "Index", id = UrlParameter.Optional }
            );
        }
    }
    
    0 讨论(0)
  • 2021-01-13 05:07

    Try checking this.ControllerContext.RouteData.DataTokens["area"] in your area's action methods.

    I've had a situation with similar issue in which the area name was simply an empty string.

    Setting the DataToken in the action method might provide you a quick fix but it doesn't answer the question why MVC doesn't set the area name.

    0 讨论(0)
  • 2021-01-13 05:08

    In your case because you specify area in query string, the routes from Global.asax.cs should apply. I belive you have something like this there:

        public override void RegisterArea(AreaRegistrationContext context)
        {
            context.MapRoute(
                "Default",
                "{controller}/{action}/{id}",
                new { controller = "Home", action = "Index", id = UrlParameter.Optional }
            ).DataTokens.Add("area","Author");
        }
    

    This code makes Home/Index default route and skips Home/Index in url. If you change controller = "Home" on something else for example controller = "Home2" you will have displayed full link for Home/Index and new default route Home2/Index. Similar apply to default routes in areas if you have them specified.

    0 讨论(0)
  • 2021-01-13 05:16

    With the @Html.ActionLink

    use this format

    @Html.ActionLink("Authors", "Index", "Home", new { Area = "Author", registrationType = "Author" }, new{})
    

    Solved my issue.

    0 讨论(0)
提交回复
热议问题