0

I am working in mvc project. I am using javascript modules in js files to perform some operations.

I am creating and using links in js files. For example accessing an image file.

var selectIcon = function() {
       return "/Images/ui/ShapefileLine16.png";
}

and I want to send request to an action method in Product controller like this.

this.getProducts = function () {
     $.get('Product/GetProjducts', {  }, this.products);
}

This codes are working on development time, visual studio IIS express.

But when I publish the project on IIS virtual directory under Default Web Site, the functions are not working. image and controller action resources can not found.

How can I access resources relatively? (@Url.ActionLink is working in MVC but I can not use it)

1
  • It has been discussed many times on SO one, two, three... Just a simple search by keywords would solve your problem. Commented May 23, 2014 at 8:25

1 Answer 1

0

It's never really a good idea to hardcode URLs, you should use Url.Content for static resources and Url.Action for controller actions

var selectIcon = function() {
    return '@Url.Content("~/Images/ui/ShapefileLine16.png")';
}

this.getProducts = function() {
    $.get('@Url.Action("GetProducts", "Products")', {}, this.products);
}
Sign up to request clarification or add additional context in comments.

2 Comments

I can use @Url helper in <script> code, but gives error in js files.
@bookmarker if you are using this code in a separate JS file then you would need to pass the URLs as parameters from the view.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.