从开发到测试到prod的API路径不同。如何避免在部署之间的更改?

时间:2022-10-24 14:15:39

Using ASP.Net WebAPI and javascript/jquery.

使用ASP。净WebAPI jquery和javascript /。

I have the following JScript code:var apiUrl = "/api/myService/myMethod/

我有以下JScript代码:var apiUrl = "/api/myService/myMethod/

  $.GetJSON(apiUrl+"/"+myValue).done(function(data){more code here});

In my dev environment this works fine. But when I move it to our test server it becomes a web application in a subdirectory called MyApp. Is there a way to tell the javascript to go to the root of the app instead of the root of the site? I want to avoid having to prepend the "/MyApp/" to the front of that string.

在我的开发环境中,这很好。但是当我将它移动到测试服务器时它就变成了一个叫做MyApp的子目录中的web应用程序。是否有办法让javascript去到应用程序的根而不是站点的根?我想避免把"/MyApp/"放在字符串的前面。

Something lie var test="~/api/myService/myMethod/"

东西测var = " ~ / api / myService myMethod /”

1 个解决方案

#1


1  

I have the following JScript code:var apiUrl = "/api/myService/myMethod/

我有以下JScript代码:var apiUrl = "/api/myService/myMethod/

Don't have such code. Never hardcode urls in an ASP.NET MVC application. You should always use helpers when dealing with urls. So for example you could have this in a global variable in your Layout:

没有这样的代码。不要在ASP中硬编码url。净MVC应用程序。在处理url时,应该始终使用helper函数。例如,你可以在布局中使用全局变量:

<script type="text/javascript">
    var apiUrl = '@Url.Route("DefaultApi", new { controller = "values", httproute = "" });';
</script>

You are undoubtedly noticing the httproute = "" route parameter I used in order to indicate to the URL helper to use the Web API routes and not your MVC routes (which are 2 entirely different things).

毫无疑问,您会注意到httproute = "" route参数,我使用这个参数是为了向URL帮助程序指示使用Web API路由,而不是使用您的MVC路由(这是两个完全不同的东西)。

But in most cases you don't even need that. The url that you are passing to this $.getJSON function could be part of your DOM. For example you never call this $.getJSON function like that in the wild. You usually call it to unobtrusively AJAXify an anchor or a form. So the url could already be part of the DOM. For example let's suppose that you wanted to AJAXify an anchor. Great, go ahead and write this anchor to be pointing to the correct url:

但在大多数情况下,你甚至不需要它。传递给这个$的url。getJSON函数可以是DOM的一部分。例如,你从不称这个为$。getJSON函数就是这样的。您通常将其称为不引人注目的ajax化锚或表单。url可能已经是DOM的一部分了。例如,让我们假设您希望AJAXify一个锚。很好,继续写这个锚指向正确的url:

@Html.RouteLink(
    "click me", 
    "DefaultApi", 
    new { httproute = "", controller = "values", id = "123" }, 
    new { id = "mylink" }
)

Cool, now AJAXify it:

酷,现在ajax化:

$(function() {
    $('#mylink').click(function() {
        $.getJSON(this.href, function(result) {
            alert('success');
        });
        return false;
    });
});

See how you no longer need to be hardcoding any urls in your javascript? You just use javascript to unobtrusively enhance what's already present in the DOM. And this is generated by proper url helpers that ensure that the correct url is created respecting your route definitions. This way you could change the pattern of your routes in a single place (wherever you configure your routes) and you never need to worry about your views.

看到你不再需要硬编码javascript中的任何url了吗?您只需使用javascript不引人注目地增强DOM中已经存在的内容。这是由正确的url助手生成的,它确保创建正确的url,尊重您的路由定义。通过这种方式,您可以在一个地方(无论您在哪里配置您的路由)更改路由模式,并且您永远不需要担心您的视图。

#1


1  

I have the following JScript code:var apiUrl = "/api/myService/myMethod/

我有以下JScript代码:var apiUrl = "/api/myService/myMethod/

Don't have such code. Never hardcode urls in an ASP.NET MVC application. You should always use helpers when dealing with urls. So for example you could have this in a global variable in your Layout:

没有这样的代码。不要在ASP中硬编码url。净MVC应用程序。在处理url时,应该始终使用helper函数。例如,你可以在布局中使用全局变量:

<script type="text/javascript">
    var apiUrl = '@Url.Route("DefaultApi", new { controller = "values", httproute = "" });';
</script>

You are undoubtedly noticing the httproute = "" route parameter I used in order to indicate to the URL helper to use the Web API routes and not your MVC routes (which are 2 entirely different things).

毫无疑问,您会注意到httproute = "" route参数,我使用这个参数是为了向URL帮助程序指示使用Web API路由,而不是使用您的MVC路由(这是两个完全不同的东西)。

But in most cases you don't even need that. The url that you are passing to this $.getJSON function could be part of your DOM. For example you never call this $.getJSON function like that in the wild. You usually call it to unobtrusively AJAXify an anchor or a form. So the url could already be part of the DOM. For example let's suppose that you wanted to AJAXify an anchor. Great, go ahead and write this anchor to be pointing to the correct url:

但在大多数情况下,你甚至不需要它。传递给这个$的url。getJSON函数可以是DOM的一部分。例如,你从不称这个为$。getJSON函数就是这样的。您通常将其称为不引人注目的ajax化锚或表单。url可能已经是DOM的一部分了。例如,让我们假设您希望AJAXify一个锚。很好,继续写这个锚指向正确的url:

@Html.RouteLink(
    "click me", 
    "DefaultApi", 
    new { httproute = "", controller = "values", id = "123" }, 
    new { id = "mylink" }
)

Cool, now AJAXify it:

酷,现在ajax化:

$(function() {
    $('#mylink').click(function() {
        $.getJSON(this.href, function(result) {
            alert('success');
        });
        return false;
    });
});

See how you no longer need to be hardcoding any urls in your javascript? You just use javascript to unobtrusively enhance what's already present in the DOM. And this is generated by proper url helpers that ensure that the correct url is created respecting your route definitions. This way you could change the pattern of your routes in a single place (wherever you configure your routes) and you never need to worry about your views.

看到你不再需要硬编码javascript中的任何url了吗?您只需使用javascript不引人注目地增强DOM中已经存在的内容。这是由正确的url助手生成的,它确保创建正确的url,尊重您的路由定义。通过这种方式,您可以在一个地方(无论您在哪里配置您的路由)更改路由模式,并且您永远不需要担心您的视图。