15
votes

I'm using Razor in MVC 4. I'm trying to make a URL from a view in an area to a controller action in another area. The following returns an empty string:

    @Url.Action("Action", "MyController", new { area = "OtherArea" })

I've done some testing with other options, and the following returns a URL, but it's wrong:

    @Url.Action("Action", "MyController", new { parameter = 1 })

The resulting URL is /App/IncorrectArea/My/Action?parameter=1. Note the incorrect area; Url.Action is assuming (since I didn't pass an area) that the controller lives in the current area which it does not!

Any ideas?

2

2 Answers

12
votes

Found my issue. I had changed the area name, but failed to update the AreaName property in the Area Registration.

1
votes

I had the same problem because I changed the RegisterRoutes method from {controller}/{action}/{id} to {action}/{id} just to emit controller name from URL