Manuel Navarro
Manuel Navarro

Reputation: 1849

How does UrlHelper.GenerateContentUrl works?

I dont get it, I have this code:

return JavaScript(string.Format(
   "window.location = '{0}'",
   UrlHelper.GenerateContentUrl("Index", this.HttpContext)));

The code is inside two pretty generic "Create" methods that works with POST. Each of the two methods are in different controller classes.

Now for method A that is called with the URL http://localhost:56688/Businessrule/Create, when the code is executed I get redirected to http://localhost:56688/Index.

But for method B called from http://localhost:56688/FormulaField/Create I get redirected to http://localhost:56688/FormulaField/Index.

... really I don't get it, and the microsoft documentation isn't helping out much http://msdn.microsoft.com/en-us/library/system.web.mvc.urlhelper.generatecontenturl.aspx (now, IMHO, that's a pretty crappy documentation for a method)

Upvotes: 2

Views: 8301

Answers (2)

Manuel Navarro
Manuel Navarro

Reputation: 1849

So, as pointed by asawyer how it works is answered by the code itself:

https://github.com/aspnet/AspNetWebStack/blob/master/src/System.Web.Mvc/UrlHelper.cs

It turns out that if the string that you pass begin with "~" the method will call PathHelpers.GenerateClientUrl, but if the string doesn't begin with "~" it will just return the same string unchanged.

But still I don't undertand why I'm getting different results. Anyway seems that I'll have to look closer to the raw response passed to the browser...

Upvotes: 0

SidAhmed
SidAhmed

Reputation: 2352

Sounds like your missing the controller name. apperently, you're being redirected to the Index action in the same controller.

It's what the MVC Route engine do, if he does not find the controller name, he assign a default value, in this case, the controller from witch the action has been executed.

Try something like :

UrlHelper.GenerateContentUrl(@"~\ControllerName\Index", this.HttpContext)

Upvotes: 2

Related Questions