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)

barteloma
  • 6,403
  • 14
  • 79
  • 173
  • It has been discussed many times on SO [one](http://stackoverflow.com/questions/13640559/asp-net-mvc-url-action-in-external-js-file), [two](http://stackoverflow.com/questions/922997/asp-net-mvc-resolve-urls-in-javascript), [three](http://stackoverflow.com/questions/2012610/generating-an-action-url-in-javascript-for-asp-net-mvc)... Just a simple search by keywords would solve your problem. – Zabavsky May 23 '14 at 08:25

1 Answers1

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);
}
James
  • 80,725
  • 18
  • 167
  • 237