serefbilge
serefbilge

Reputation: 1714

Asp .Net MVC4 website on IIS 7.5 gets HTTP 404 error in ajax error response

function girisAjaxKontrol() {
var kullanici = { 'kullaniciAdi': $('#username').val(), 'hidden': $('#password').val() };
$.ajax({
    url: '/Giris/GirisGecerliMi',
    type: 'POST',
    data: kullanici,
    success: girisAjaxReturn,
    error: function (error, textstatus) {
        JSON.stringify(error);
        errorMessage($("div.girisSubmit input"), JSON.stringify(error), false);            
    }
});

}

This function gets the error below(it was working before deployment). The website is an Asp .Net MVC4 website on local IIS 7.5; I searched a lot but couldn't solve yet.

Server Error in Application \"DEFAULT WEB SITE\"

HTTP Error 404.0 - Not Found

The resource you are looking for has been removed, had its name changed, or is temporarily unavailable.

Module: IIS Web Core
Notification: MapRequestHandler
Handler: StaticFile
Error Code: 0x80070002

Requested URL   http://localhost:80/Giris/GirisGecerliMi
Physical Path   C:\\inetpub\\wwwroot\\Giris\\GirisGecerliMi
Logon Method    Anonymous
Logon User  Anonymous   

Upvotes: 3

Views: 3254

Answers (1)

Darin Dimitrov
Darin Dimitrov

Reputation: 1038720

url: '/Giris/GirisGecerliMi',

should not be hardcoded like this. You should use url helpers to generate this url. The reason for that is because when you deploy your application in IIS there's a virtual directory name that you should take into account, so the correct url is:

url: '/MyAppName/Giris/GirisGecerliMi',

But by hardcoding the url as you did there's no way for this to work. In ASP.NET MVC you should always use url helpers such as Url.Action to generate the url to a given controller action as those helpers take into account not only your routing definitions but things like virtual directory names as well.

So the correct way is to have this url generated server side and passed as argument to the girisAjaxKontrol function:

function girisAjaxKontrol(url) {
    var kullanici = { 'kullaniciAdi': $('#username').val(), 'hidden': $('#password').val() };
    $.ajax({
        url: url,
        type: 'POST',
        data: kullanici,
        success: girisAjaxReturn,
        error: function (error, textstatus) {
            JSON.stringify(error);
            errorMessage($("div.girisSubmit input"), JSON.stringify(error), false);            
        }
    });
}

Upvotes: 5

Related Questions