amesh
amesh

Reputation: 1329

MVC Deployment - Relative Path issue with respect to Application Path

While deploying my MVC Project, I have faced an issue of relative path w.r.t server. I am hosting the project as an application in IIS. Finally the url to my application will look like http://localhost/portal/Account/Login here 'portal' is the application name in IIS. In the ASP.net development server everything was working fine. while deploying it needed the relative path with respect to server. Because of this my jquery ajax requests started failing. To fix this issue I kept the actions in hidden field and accessing from there and using for ajax request. The following is the code.

 <input type="hidden" value="@Url.Action("GetNewOffersSearch", "Updates")" id="NewOffersSearchUrl" />

var NewoffersUrl = document.getElementById("NewOffersSearchUrl").value;


 $.ajax({
            type: 'GET',
            url: NewoffersUrl ,
            cache: false,
            timeout: 10000,
            contentType: "application/json; charset=utf-8",
            success: function (_results) {
                $("#updatesContent").html(_results);
            },
            error: function (_results) {

            }
        });

Initially NewoffersUrl was "/Updates/GetNewOffersSearch" and it was throwing a path error. But now it is "/portal/Updates/GetNewOffersSearch" and its working fine

I just want to know the approach I am following is correct or not. Is there any better fixes for this issue?

Upvotes: 3

Views: 1167

Answers (1)

Darren
Darren

Reputation: 70806

The way we do AJAX requests is similiar, however we pass the URL directly to the url parameter of the ajax call rather than using a hidden field.

$.ajax({
        type: 'GET',
        url: @Url.Action("GetNewOffersSearch", "Updates"),
        cache: false,
        timeout: 10000,
        contentType: "application/json; charset=utf-8",
        success: function (_results) {
            $("#updatesContent").html(_results);
        },
        error: function (_results) {

        }
    });

Upvotes: 3

Related Questions