Reputation: 6766
I have asp.net mvc application deployed as application under one website in IIS.
Controller:
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);
}
View:
<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)
);
});
});
}
});
When I deployed it to IIS as an application under website it loads the cities properly. URL was like http://www.example.com/applicationname/BookingRequest
. and When I change the country dropdown value it executes get on http://www.example.com/applicationname/BookingRequest/GetCities/2
and all things are working fine.
but when I hit run on the visual studio it and change the country it executes getCities on wrong URL: http://localhost:506084/BookingRequest/BookingRequest/GetCities/2
My question is why this behaves differently on the localhost running from the visual studio and on the IIS? Is it due to the deployment as web application inside the website at IIS? Or is it anything to do with my browser?
Here is snap of that request from web developer tool of IE.
Upvotes: 0
Views: 1477
Reputation: 129
You need to specify the base url of you application. To do that, you need to add tag on each of your pages or may on _Layout page.
For example:
<base href="Root URL of you aplication" />
Please note that this should match the root url you are seeing the browser.
This way your application will always refer to correct root path.
Upvotes: 0
Reputation: 1242
To get the correct relative path in MVC below are the following thing we need to do.
In MVC View :
<script>
var strUrl = '@Url.Action("actionName", "controllerName")';
</script>
In Javascript:
Get that variable "strUrl" and use in your ajax code.
$.ajax({
type: "GET",
url: strUrl ,
contentType: "application/json; charset=utf-8",
dataType: "html",
success: function (data) {
});
Upvotes: 2
Reputation: 64160
Seem like you have entered the wrote url for IIS Express in your Project settings.
Open the Properties of your Web project and go to the "Web" tab and change the "Project Url" to from http://localhost:506084/BookingRequest/
to http://localhost:506084/
or http://localhost:506084/applicationname
for the sake of it.
But it doesn't matter if it's in a application folder or not, urls in MVC Webapps should always be relative to application path (using ~/Relative/Url/Here
).
That being said... don't hardcode your root url inside your code. Use return Redirect("/")
(domain root) or return Redirect("~/")
(application root).
Upvotes: 1