【问题标题】:Running MVC application on IIS behaves differently than on running Visual studio localhost在 IIS 上运行 MVC 应用程序的行为与运行 Visual Studio localhost 不同
【发布时间】:2015-04-06 10:50:39
【问题描述】:

我将 asp.net mvc 应用程序部署为 IIS 中一个网站下的应用程序。

控制器:

public ActionResult Index()
{
    List<SelectListItem> ddlCountry = countryService.GetCountries().Select(d => new SelectListItem() { Text = d.Name, Value = d.Id.ToString() }).ToList();
    ddlCountry.Insert(0, new SelectListItem() { Text = string.Empty, Value = string.Empty });
    ViewData["ddlCountry"] = ddlCountry;
}

public ActionResult GetCities(int Id)
{
    if (!Convert.ToBoolean(ConfigurationManager.AppSettings["InDev"]))
    {
        return Redirect("http://example.com/");
    }
    var list = cityService.GetCities(Id).Select(d => new {Id = d.Id, Name = d.Name }).ToList();
    return Json(list, JsonRequestBehavior.AllowGet);
}

查看:

<tr>
    <td>@Html.LabelFor(model => model.CountryId, "Country Name")</td>
    <td>@Html.DropDownListFor(model => model.CountryId, ViewData["ddlCountry"] as List<SelectListItem>)</td>
    <td>@Html.ValidationMessageFor(model => model.CountryId)</td>
</tr>

<tr>
    <td>@Html.LabelFor(model => model.CityId, "City Name")</td>
    <td>@Html.DropDownListFor(model => model.CityId, ViewData["ddlCity"] as List<SelectListItem>)</td>
    <td>@Html.ValidationMessageFor(model => model.CityId)</td>
</tr>

JavaScript:

$(document).ready(function () {

    $('#CountryId').change(function () {
        loadDropDown("CityId", "BookingRequest/GetCities/" + $('#CountryId').val())
        $('#AreaId').html($('<option></option>').val("").html(""));
    });

    function loadDropDown(destinationId, Url) {
        $.getJSON(Url, function (data) {
            $('#' + destinationId).html($('<option></option>').val("").html(""));
            $.each(data, function (index, item) {
                $('#' + destinationId).append(
                     $('<option></option>').val(item.Id).html(item.Name)
                 );
            });
        });
    }
});

当我将它作为网站下的应用程序部署到 IIS 时,它会正确加载城市。 URL 就像http://www.example.com/applicationname/BookingRequest。并且当我更改国家/地区下拉值时,它会执行 get on http://www.example.com/applicationname/BookingRequest/GetCities/2 并且一切正常。

但是当我在 Visual Studio 上运行它并更改它在错误 URL 上执行 getCities 的国家/地区时:http://localhost:506084/BookingRequest/BookingRequest/GetCities/2

我的问题是为什么这在从 Visual Studio 运行的 localhost 和 IIS 上表现不同?是因为在 IIS 的网站内部署为 Web 应用程序吗?还是和我的浏览器有关?

这是来自 IE 的 Web 开发工具的请求的快照。

【问题讨论】:

  • 您没有传递正确的(相对)网址。遵循标准做法并使用'@Url.Action("GetCities", "BookingRequest")',以便正确生成您的 url'(在这种情况下使用前导斜杠 - /BookingRequest/GetCities
  • @StephenMuecke 但是那个 ajax 调用在不同的 javascript 文件中。那么我将如何使用@URL helper 在 javascript 文件中生成 url。以下 Jinesh 的回答是否被认为是最佳实践?
  • 您可以这样做,尽管最常见的解决方案是将 url 添加到元素 data- 属性中 - 例如@Html.DropDownListFor(model =&gt; model.CountryId, ...... new { data_url = "Url.Action("..", "..") }) 然后通过 change() 事件访问它 - 例如var url = $(this).data(url);
  • 在项目属性 > Web > 单击单选按钮“使用本地 IIS 服务器”。通过这样做,您的两种行为将是相同的。

标签: c# jquery ajax asp.net-mvc


【解决方案1】:

要在下面的 MVC 中获得正确的相对路径,我们需要做以下事情。

在 MVC 视图中:

<script>
var strUrl = '@Url.Action("actionName", "controllerName")';
</script>

在 Javascript 中:

获取该变量“strUrl”并在您的 ajax 代码中使用。

$.ajax({
 type: "GET",
 url: strUrl ,
 contentType: "application/json; charset=utf-8",
 dataType: "html",
 success: function (data) {

});

【讨论】:

  • 虽然我会采用斯蒂芬在问题评论中提到的解决方案,但我喜欢你将 url 传递给 javascript 的方式。
【解决方案2】:

您似乎在项目设置中输入了 IIS Express 的写入网址。

打开您的 Web 项目的属性并转到“Web”选项卡并将“Project Url”从http://localhost:506084/BookingRequest/ 更改为http://localhost:506084/http://localhost:506084/applicationname

但不管它是否在应用程序文件夹中,MVC Webapps 中的 url 应该始终相对于应用程序路径(使用 ~/Relative/Url/Here)。

话虽如此...不要在代码中硬编码您的根 URL。使用return Redirect("/")(域根)或return Redirect("~/")(应用程序根)。

【讨论】:

  • 我的项目网址是:localhost:50684。我完全同意您的评论 MVC 处理与应用程序路径相关的所有请求。但是为什么我会出现这种奇怪的行为呢?
  • BookingRequest 您的控制器和您的 java 脚本是否嵌入在同一个控制器中?那么你不需要BookingRequest/GetCities 中的BookingRequest 作为你的java 脚本url 的一部分,因为你已经在正确的页面上。否则,如果您从其他控制器/操作访问它,则必须从应用程序根目录进入
  • 是的 javascript 文件在同一页面上,但如果我从 url 部分删除 BookingRequest,它在 Visual Studio 下可以正常工作,但随后停止在 IIS 上工作,因为 URL 类似于 http://www.example.com/applicationname/GetCities/2。 :(
  • @jenish - 当解决方案在 iis 下运行时,它表示它在某个端口上临时托管应用程序并作为网站运行,如果我们将相同的应用程序部署为虚拟目录,那么路径问题就会出现。
  • 感谢您的帮助。决定采用斯蒂芬评论中提到的解决方案。
【解决方案3】:

您需要指定应用程序的基本 url。为此,您需要在每个页面上添加标签,或者可能在 _Layout 页面上添加标签。

例如:

<base href="Root URL of you aplication" />

请注意,这应该与您在浏览器中看到的根 url 匹配。

这样您的应用程序将始终引用正确的根路径。

【讨论】:

  • 您似乎不确定这个答案,或者您发布了它,但没有完成。也许你想删除这个答案?
猜你喜欢
  • 2017-06-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-09-25
  • 2012-05-27
  • 2010-10-18
  • 2020-06-28
相关资源
最近更新 更多