无法确定 URL 为何不包含 IIS 中的应用程序文件夹

发布于 2024-12-08 21:45:32 字数 1081 浏览 0 评论 0原文

假设该应用程序物理上位于:

C:\inetpub\wwwroot\MyApplication

这已通过 IIS 7.5 转换为应用程序,我现在可以通过...访问该应用程序

http://localhost/MyApplication 

,因为它将使用默认路由。如果我打电话给...

http://localhost/MyApplication/MyRequest

...则使用相同的路线并提供正确的页面。问题是上述 URL 是一个表单,提交该表单后,我在同一个控制器中调用一个操作,但没有相应地路由。生成的 URL 是...

http://localhost/MyRequest/MyMethod

与...

http://localhost/MyApplication/MyRequest/MyMethod

应用程序内的唯一路由是...

    routes.MapRoute(
        "Default", // Route name
        "{controller}/{action}/{id}", // URL with parameters
        new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
    );

这是路由问题吗?驻留在 Scripts 文件夹中的 JS(jqueryUI 等...)也没有被加载,就好像所有内容都设置为驻留在层次结构中的根级别,并且添加到 IIS 中的 MyApplication 文件夹中已经抛出了一些东西环形。

更新

表单定义看起来像......

<form class="..." action="/Request/Add" method="post" id="requestForm">

Assume that the application is physically located at:

C:\inetpub\wwwroot\MyApplication

This has been converted into an application via IIS 7.5 and I am now able to access the application via...

http://localhost/MyApplication 

...as it will engage the default route. If I make a call to...

http://localhost/MyApplication/MyRequest

...the same route is engaged and the proper page is served up. The issue is that aforementioned URL is a form and upon submitting that form I call an action within the same Controller, yet am not routed accordingly. The resulting URL is...

http://localhost/MyRequest/MyMethod

versus...

http://localhost/MyApplication/MyRequest/MyMethod

The only route within the application is...

    routes.MapRoute(
        "Default", // Route name
        "{controller}/{action}/{id}", // URL with parameters
        new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
    );

Is this a routing issue? The JS (jqueryUI, etc...) which resides in the Scripts folder is also not being loaded, it's as if everything is set to reside at the root level within the hierarchy and adding in the MyApplication folder within IIS has thrown things for a loop.

UPDATE:

The form definition looks like...

<form class="..." action="/Request/Add" method="post" id="requestForm">

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

杯别 2024-12-15 21:45:32

我敢打赌,您的视图和脚本中已经硬编码了 url,而不是使用助手。

例如,关于CSS,不要像这样硬编码它:

<link href="/Content/Site.css" rel="stylesheet" type="text/css" />

使用url helpers:

<link href="<%= Url.Content("~/Content/Site.css") %>" rel="stylesheet" type="text/css" />

关于你的HTML表单和锚点总是使用HTML helper来生成它们:

<% using (Html.BeginForm()) { %>
    ...
<% } %> 

关于你的javascript文件绝对不要像这样硬编码任何url:

$.ajax({
    url: '/foo/bar',
    ...
});

你应该始终依赖url 在 ASP.NET MVC 应用程序中处理 url 时提供帮助程序。现在,无论您的应用程序托管在何处以及您的路线如何,它都可以正常工作。


更新:

现在看到您的更新后,

 <form class="..." action="/Request/Add" method="post" id="requestForm">

您应该使用 html 帮助程序来生成它们,而不是对表单进行硬编码:

<% using (Html.BeginForm("Add", "Request", null, FormMethod.Post, new { id = "requestForm", @class = "foo" })) { %>
    ...
<% } %> 

I bet you have hardcoded urls in your views and scripts instead of using helpers.

For example concerning the CSS, instead of hardcoding it like this:

<link href="/Content/Site.css" rel="stylesheet" type="text/css" />

use url helpers:

<link href="<%= Url.Content("~/Content/Site.css") %>" rel="stylesheet" type="text/css" />

and concerning your HTML forms and anchors always use HTML helper to generate them:

<% using (Html.BeginForm()) { %>
    ...
<% } %> 

and concerning your javascript files absolutely never hardcode any urls like this:

$.ajax({
    url: '/foo/bar',
    ...
});

You should always rely on url hepers helpers when dealing with urls in an ASP.NET MVC application. Now, no matter where your application is hosted and how your routes look like, it will work.


UPDATE:

And now after seeing your update, instead of hardcoding your forms:

 <form class="..." action="/Request/Add" method="post" id="requestForm">

you should use html helpers to generate them:

<% using (Html.BeginForm("Add", "Request", null, FormMethod.Post, new { id = "requestForm", @class = "foo" })) { %>
    ...
<% } %> 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文