JQuery ASP.NET MVC - $ .post () for a different URL path in different environments

Consider the need $.post()for a slightly different URL structure in the Visual Studio Dev environment and in the IIS deployment or testing environment.

When deployed to a test server, the application runs under a virtual directory in IIS. The URL will look something like this:

Deployed

URL: http://myServer/myApplication/Area/Controller/Action

Using jQuery .post(), I need to specify a line:

 $.post( "/myApplication/myArea/myController/myMethod"

Development

In Visual Studio

Cassini URL: http://localhost:123/Area/Controller/Action

Using jQuery .post(), I need to specify a line:

 $.post( "/myArea/myController/myMethod"

Question: How can I get both of them to use the same line of code, regardless of their deployed environment?

+3
source share
2 answers

, URL- RouteUrl :

var url = "<%= Url.RouteUrl(new { area="myArea", controller = "controller", action = "actionmethod" }) %>";
$.post(url ...

, Url.

: .

+3

( ?) js- :

<script type="text/javascript" >
    var globalAppPath = '<%= Request.ApplicationPath %>';
</script>

URL-.

$.post( globalAppPath + "/myArea/myController/myMethod"

, -.

+1

Source: https://habr.com/ru/post/1719426/


All Articles