1

I am developing asp.net mvc project. I have a javascript object that sends query to controller action. in cshtml file I can use @Url.Action("get", "product") so when I publish the web site, url action is rendering by location url. If I publish it http://localhost/App1/, the action url is like this http://localhost/App1/product/get or I can publish it another directory like http://localhost/App2/ and so on.

But I have an issue in javascript code.

sample.js

function query(){
    var url = "/product/get";

    // send query this url
} 

When I publish the project in http://localhost/App1/ url (APP1 folder), javascript query is sending request to http://localhost/product/get , but it should be like this http://localhost/App1/product/get

I can not use @Url.action() razor expression. How can I solve this issue?

2
  • How are you using query()? Commented Mar 19, 2014 at 12:33
  • Why can not you use @Url.action() razor expression if it is mvc 4 application? Commented Mar 19, 2014 at 12:45

2 Answers 2

1

You can place root level appPath variable in your _layout.cshtml

<script type="text/javascript">
    var appPath = @Url.Content("~/");
</script>

Then in your query

function query(){
    var url = appPath +"/product/get";
}
Sign up to request clarification or add additional context in comments.

Comments

1

I send the url as a parameter from the View to the javascript function which is written in a js file. This saves you the trouble of creating a url in js and you can use you razor expression for the same.

So your function becomes:-

function query(url){
//var url = "/product/get";
// send query this url
}

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.