5

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("Register as a Client", "Register", "Account", new { registrationType = "Client"}, null)

link works fine, and the link opens the correct page. However, with a link to an area based action like this:

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

the link actually rendered to the browser is missing action and controller, i.e.

http://mylivedomain.com/?Area=Author&registrationType=Author

It may be worth noting that the css bundling feature of MVC4 was not working after deployment, and I rolled back to using classic style links to individual stylesheets.

MAYBE RELATED: My question: Why is unmodified template code in my MVC4 app trying to register areas twice?

JUST IN: Removing the default action from the area route mappings for the default rouite solved this problem. There was no default controller to start with, in the VS2012 template code.

Community
  • 1
  • 1
ProfK
  • 49,207
  • 121
  • 399
  • 775
  • Did you forget to use a `new{}` at the end of your ActionLink()? http://stackoverflow.com/questions/2036305/how-to-specify-an-area-name-in-an-action-link – Nick DeVore Jul 17 '12 at 21:28
  • @NickDeVore, I'm using a `null` instead, and this works for me on my dev machine, and worked before on the previous, MVC3, version of my site that I'm deploying. – ProfK Jul 17 '12 at 21:59
  • Sounds fine, I had just found that other question and the answer seemed to imply it was now required? Anyway, just a thought. – Nick DeVore Jul 18 '12 at 01:54
  • Is the only "HomeController" in your entire project in that Authors area? I get an error with more than one HomeController in the project, but maybe you're suppressing that error somehow? – jaminto Jul 22 '12 at 22:47
  • @jaminto I have a HomeController in every area in the project, and am definitely not suppressing an error that I know of. Like I say, this all works on my dev machine, only get issues when I deploy. – ProfK Jul 23 '12 at 06:35
  • So you're specifying a namespace in every route in your area registration? Just wondering if you have the same problem with a controller that has a unique name across your entire project and all areas. – jaminto Jul 23 '12 at 12:50
  • @SergRogovtsev No, this is a dead standard, out-of-box MVC4 project, based on the project template that comes with VS2011. – ProfK Jul 26 '12 at 07:30
  • Are you 100% sure that you're deleting all dlls in the deployment directory before you deploy? – jaminto Jul 23 '12 at 12:54

5 Answers5

2

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.

Jani Hyytiäinen
  • 5,293
  • 36
  • 45
2

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.

jan salawa
  • 1,198
  • 1
  • 8
  • 25
  • Explicitly adding the data token causes an "An item with the same key has already been added." exception. – ProfK Aug 16 '12 at 12:48
1

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 }
        );
    }
}
Paul Fleming
  • 24,238
  • 8
  • 76
  • 113
1

With the @Html.ActionLink

use this format

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

Solved my issue.

Pogrindis
  • 7,755
  • 5
  • 31
  • 44
0

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.

Jani Hyytiäinen
  • 5,293
  • 36
  • 45