John Edwards
John Edwards

Reputation: 1546

ASP MVC Relative URL Path(not file)

I have a link to <a href='/ViewReport'> on my local host that works fine, but on the server the whole site is in a folder "serverfolder", so the link becomes http://somesite/serverfolder/ViewReport, which isn't a valid url. I have seen how to use ~ to access the root directory for files, but not how to do this with url paths. I want to use the same link for both local and remote deployment. How would I achieve this? Thank you!

Upvotes: 1

Views: 1185

Answers (1)

ataravati
ataravati

Reputation: 9155

Do this:

var urlHelper = new UrlHelper(Request.RequestContext);
string url = Request.Url.GetLeftPart(UriPartial.Authority) 
   + urlHelper.Action("ViewReport", 
     new { userId = UserName, reportId = PI.ElementAt(i).TempUserID });

Or, if you prefer not to use the UrlHelper.Action, you do it like this:

string url = Request.Url.GetLeftPart(UriPartial.Authority) + "/ViewReport...";

Upvotes: 5

Related Questions