在本地开发我的应用程序 (asp.net mvc3) 时,使用 VS 开发应用程序服务器一切都很好。该应用程序位于 localhost/。但是,我尝试将应用程序部署在 /Management 目录中的 IIS 7.5 服务器上,并且存在很多路由问题,因为我的应用程序中的一些调用依赖于路由中的应用程序。
我有一些 javascript 代码,通过 ajax 调用调用我的控制器,如下所示:
url: "/en/Home/GetFormula/"
我希望它转到:/Management/en/Home/GetFormula,但它会转到网站的根目录并查找 /en/ Home/GetFormula 并返回 404 错误。
关于如何将我的 javascript 路由修复为默认 /Management/ 作为网站的根目录,有什么想法吗?
谢谢
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.
I have some javascript code that calls my controller through an ajax call that looks like this:
url: "/en/Home/GetFormula/"
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.
Any ideas on how I can fix my javascript routing to default /Management/ as the root of the site?
Thanks
发布评论
评论(3)
当传递给 HTTP 请求的伪 URL 以斜杠开头(例如“/path/to/resource”)时,通过将其视为给定路径位于网站的根目录(例如“http://my.site/path/to/directory”)。
显然,您希望处理伪 URL,就好像给定路径位于 Web 应用程序的根目录下一样。好吧,我有一个坏消息要告诉你:HTTP 协议不处理“Web 应用程序”这样的事情。
ASP.NET MVC 框架提供了
Url.Content
函数,该函数采用以波形符开头的伪 URL(例如“~/path/to/resource”)并返回替换Web 应用程序根目录的波形符(例如,“http://my.site/an/application/path/to/resource”,假设 Web 应用程序的根目录为“http:/my.site/an/application” )。但是,ASP.NET MVC 框架仅在服务器端可用。如果您的 JavaScript 在客户端运行,则它无法调用Url.Content
。但并非一切都失去了。 ASP.NET MVC 框架 允许您动态生成服务器上的 JavaScript 代码并在客户端上运行它,方式与允许您动态生成 HTML 内容,当然也可以将其发送到客户端。这样,您就可以在服务器端将伪 URL 扩展为实际 URL,并将生成的 JavaScript 代码部署到客户端。
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").
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".
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 callUrl.Content
.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.
为了避免混淆您当前正在尝试的位置:
To avoid confusion about where you are currently try:
我解决了这个问题,在我的页面上添加了一个 html 隐藏字段,在服务器端,我放置了使用 Url.RouteUrl 方法推断出的正确 url,如下所示:
然后,在您的 javascript 代码中,您可以执行以下操作:
I solved this issue adding a html hidden field on my page where, on the server side, I put the correct url inferred with the Url.RouteUrl method like this:
then, on your javascript code you could do this: