4

I have an ASP.NET MVC app. I am using Razor in my views. Due to my IT dept. the app sits at a relative root like http://ourdomain.com/appRoot instead of at the top of the domain like http://ourdomain.com. The other challenge here is, appRoot is not static. It actually changes. For that reason, I need to get the name of the relative root via Razor. Currently, I have something like the following:

<input id="hiddenElem" type="hidden" value="12345" />
<script type='text/javascript'>
  function doThis() {
    var id = $('#hiddenElem').val();
    var nextUrl = '@(Request.?)' + '/path/' + id;
    alert(nextUrl);
  }
</script>

When doThis is called, I want to see the alert window to display /appRoot/path/12345. What do I put into the Razor block associated with nextUrl to get the relative root?

1 Answer 1

6

Generally you would use the mvc routing function Url.Action().

function doThis() {
    var id = $('#hiddenElem').val();
    var nextUrl = '@Url.Action("MyAction","MyController")' + '/path/' + id;
    alert(nextUrl);
  }

The Html.ResolveUrl() function is fine for resolving relative paths, however, it does not take into consideration routing.

Also, using the Url.Content() function may produce a result you are expecting. For example, in the snippet below:

.Template("<img src='"+ @Url.Content("~/Images/Icons/16/STATE.png")+"'/> <mark>${data.StateName }</mark>");

The image will always resolve no matter where you are calling it from or how you accessed the view withing the root.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.