asp.net MVC路由 - JavaScript网址404错误 [英] asp.net MVC routing - javascript url 404 errors

查看:325
本文介绍了asp.net MVC路由 - JavaScript网址404错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在开发我的应用程序(asp.net MVC3)局部一切都很好使用VS开发应用服务器。该应用程序位于本地主机/。不过,我尝试部署在/管理目录中的IIS 7.5服务器上的应用程序,并拥有大量的路由问题在我的应用程序打了几个电话依赖于应用程序处于的路由。

While developing my app (asp.net mvc3) locally everything was fine using the VS dev app server. The app was located at localhost/. However, I'm attempting to deploy the application on a IIS 7.5 server in a /Management directory and having a lot of routing issues as a few calls in my app rely on the app being at the route.

我有一些javascript code,通过Ajax调用看起来像这样叫我的控制器:

I have some javascript code that calls my controller through an ajax call that looks like this:

url: "/en/Home/GetFormula/"

我想它去:/管理/ EN /主页/ GetFormula而是它要在网站的根目录,并寻找/ EN /主页/ GetFormula并返回404错误

I would like it to go to: /Management/en/Home/GetFormula but instead it's going to the root of the site and looking for /en/Home/GetFormula and returning 404 errors.

我如何能解决我的JavaScript路由任何想法默认/管理/作为网站的根?

Any ideas on how I can fix my javascript routing to default /Management/ as the root of the site?

感谢

推荐答案

当传递到一个HTTP请求伪URL以斜杠开头(如/路径/到/资源),该伪URL是已完成通过将其视为给出的路径是Web站点的根目录下(如http://my.site/path/to/directory)。

When the pseudo-URL passed to an HTTP request begins with a slash (e.g. "/path/to/resource"), the pseudo-URL is "completed" by treating it as if the given path were under the Web site's root directory (e.g. "http://my.site/path/to/directory").

显然,您所期望的伪URL,如果给出的路径是你的Web应用程序的根目录下进行处理。好了,我对你不好的消息:HTTP协议中没有这样的事情处理为Web应用程序

Clearly, you were expecting the pseudo-URL to be processed as if the given path were under your Web application's root directory. Well, I have bad news for you: The HTTP protocol does not deal with such a thing as a "Web application".

ASP.NET MVC框架提供了 Url.Content 函数,该函数假网址,以波形符(例如,〜/路径/到/资源开始),并返回与Web应用程序的根目录替换波形符(例如,结果http://my.site/an/application/path/to/resource,假设Web应用程序的根目录为http: /my.site/an/application)。但是,ASP.NET MVC框架仅仅是在服务器端使用。如果您的JavaScript在客户端上运行时,它不能称之为 Url.Content

The ASP.NET MVC Framework provides the Url.Content function, which takes pseudo-URLs beginning with a tilde character (e.g., "~/path/to/resource") and returns the result of replacing the tilde character with the Web application's root directory (e.g., "http://my.site/an/application/path/to/resource", assuming the Web application's root directory is "http:/my.site/an/application"). However, the ASP.NET MVC Framework is only available on the server side. If your JavaScript runs on the client side, it cannot call Url.Content.

但并非所有的都将丢失。 ASP.NET MVC框架允许你动态地生成在服务器上的JavaScript code和在客户运行它,同样的方式,它的允许你动态生成HTML内容,当然它发送到客户端。这样一来,可以扩大伪URL转换成实际的URL的服务器端的,并部署产生的JavaScript code到客户端。

But not all is lost. The ASP.NET MVC Framework allows you to dynamically generate JavaScript code on the server and run it on the client, the same way it allows you to dynamically generate HTML content and of course send it to the client. That way, you can expand the pseudo-URLs into actual URLs on the server side, and deploy the resulting JavaScript code to the client.

这篇关于asp.net MVC路由 - JavaScript网址404错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆