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

Controller name randomly gets added to URLs

I have a controller class called

and client javascript code like

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

Sometimes it works correctly using the URL
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"POST", 'Home/UploadImage', false);

then sometimes it works and uses
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
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"POST", '/Home/UploadImage', false);

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