Domanda

I have a very standard ASP MVC app that I use a little javascript to show a Partial View. In order to make that Javascript work I needed to hard code a path to the Partial which is different between Dev and Production.

Mainly, in Dev there is no App specification where as in Production there is. See here:

Production=var URL = '/WetWashRequest/wetWashRequests/GetDetails?WONumber=' + wo;

Dev = var URL = '/wetWashRequests/GetDetails?WONumber=' + wo;

What this means is that as I work on it locally I delete the first part and when I want to deploy I have to remember to re add it.

This seems so ridiculously flawed that I can only assume I am being ignorant and doing something wrong...

È stato utile?

Soluzione

You can take advantage of UrlHelper to get the URLs, as long as you do it in view:

var URL = '@Url.Action("GetDetails")';

Obviously, it doesn't make sense to put all your JavaScript in view, so what I will normal do is set just this in my view, in a namespace var, and then reference it in my external JavaScript:

View

<script>
    var MyApplication = MyApplication || {};
    MyApplication.GetDetailsUrl = '@Url.Action("GetDetails")';
</script>

External JS

$.get(MyApplication.GetDetailsUrl, { WONumber: wo }, function (result) {
    ...
});
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top