user1318499 user1318499 - 1 year ago 73
ASP.NET (C#) Question

Controller name randomly gets added to URLs

I have a controller class called

HomeController
and client javascript code like

function upload(content) {
var ajax = new XMLHttpRequest();
ajax.open("POST", 'UploadImage', false);
ajax.setRequestHeader('Content-Type', 'application/upload');
ajax.send(content);
}


Sometimes it works correctly using the URL
https://localhost/MyApp/Home/UploadImage
and uploads the data. But sometimes it fails because there's no controller name in the URL. Chrome developer tools shows the error as:

POST https://localhost/MyApp/UploadImage 404 ()


But if I insert /Home in the URL myself, changing the above code to

ajax.open("POST", 'Home/UploadImage', false);


then sometimes it works and uses
https://localhost/MyApp/Home/UploadImage
but it sometimes fails with

POST https://localhost/MyApp/Home/Home/UploadImage 404 ()


If I recompile and start again, sometimes it changes to the opposite behavior but I can't predict when. The same thing happens with a GET request. I don't know if I should include the controller name
Home/
in my URLs or not. How can I make it behave consistently?

Answer Source

this is due to relative URL only,

it will change with respect to code or page your using.. so solution for this is either u choose

ajax.open("POST", '/Home/UploadImage', false);

or

ajax.open('POST', '@Url.Action("UploadImage","Home")', false);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download