I’m using MVC3 and building an a tag (link) using jquery. The problem is I can only get my links to work in either prod or dev, not both. Here is my site’s url in production.
This is my development url.
http://localhost/TVAP
So, I have a link that I’m creating via jquery as follows.
var actionCell = $('#itemlist').find(rowId).find(actionCellId);
$('<a>Edit</a>').attr({ 'id': 'edit-' + response.id, 'href': '/Items/Edit/' + response.id }).appendTo(actionCell);
In production, this works, and creates a link as
<a id="edit-17" href="/Items/Edit/17">
But in development (local) it doesn’t work as I need this.
<a id="edit-17" href="/TVAP/ItemManagement/Edit/17">
I get the following from
$(location).attr('host') ==> localhost
$(location).attr('host') ==> http://localhost/TVAP/Items/Index
Either too little, or too much info. I’d know I can check for localhost and append TVAP as needed, but this seems like a hack. Another developer may use the ASP.NET VS dev server and get a URL will contain localhost but without the TVAP as I’m getting.
i.e. – http://localhost:64301/Items/Index
This is what I always do in my _Layout.cshtml:
Then you can always use stuff like
myProjectName.baseUrl+"Controller/Action"for accessing your resources from your own javascript files. This helps you separate the views from the javascript, which is a really good thing.You don’t want to mix markup and javascript – and this is a compromise that goes a long way.