MrProgram
MrProgram

Reputation: 5242

Can't find controller method when deployed

Controller:

    [HttpPost]
    public ActionResult FileExist(string fileName)
    {
        bool result = true;
        string path = Server.MapPath(TempPath) + fileName + ".xlsx"; //Path for the file
        string[] Files = Directory.GetFiles(Server.MapPath(TempPath));
        for (int i = 0; i < Files.Length; i++)
        {
            if (path == Files[i])
            {
                //The filename already exists
                result = false;
            }
        }
        return Json(new { returnvalue = result });
    }

My JS:

$(document).on('click', '#saveButton', function () {
var fileName = $('#fileName').val();
$.ajax({
    type: 'POST',
    url: '/Calculation/FileExist',
    data: { 'fileName': fileName },
    dataType: 'JSON',
    success: function (result) {
        if (!result.returnvalue) {
            $('#errorMessage').text("The filename already exists. Please choose another one (The files are automatically deleted every second day)");
            $('#downloadButton').css("visibility", "hidden");
        }
    }
});
});

When I debug this in VS it works fine. But when I have Published and put it up on a server it wont work.

My error looks like this:

Why is this not working when published?

Upvotes: 1

Views: 1904

Answers (2)

Vivek Parekh
Vivek Parekh

Reputation: 1085

Considering that Calculation is your Controller Class, try to give the ajax request as following

$.ajax({
type: 'POST',
url: '@Url.Action("FileExist","Calculation")',,
data: { 'fileName': fileName },
dataType: 'JSON',
success: function (result) {
    if (!result.returnvalue) {
        $('#errorMessage').text("The filename already exists. Please choose another one (The files are automatically deleted every second day)");
            $('#downloadButton').css("visibility", "hidden");
        }
    }
});

And if this event is written in a seperate js file then here is a code for that.

In your View make a js function as

function GetUrlToCheckIfFileExist(){
    return '@Url.Action("FileExist","Calculation")';
}

And in your js file use the following code

var getUrlToCheckIfFileExist = window.GetUrlToCheckIfFileExist();

$.ajax({
type: 'POST',
url: getUrlToCheckIfFileExist,
data: { 'fileName': fileName },
dataType: 'JSON',
success: function (result) {
    if (!result.returnvalue) {
        $('#errorMessage').text("The filename already exists. Please choose another one (The files are automatically deleted every second day)");
            $('#downloadButton').css("visibility", "hidden");
        }
    }
});

Upvotes: 2

Jitendra Pancholi
Jitendra Pancholi

Reputation: 7562

Make the url in view and set in a javascript variable and use it in your js file.

for cshtml file

<script>
 var urlpath = '@Url.Action("FileExist","Calculation")';
</script>

for js file

$(document).on('click', '#saveButton', function () {
var fileName = $('#fileName').val();
$.ajax({
    type: 'POST',
    url: urlpath,
    data: { 'fileName': fileName },
    dataType: 'JSON',
    success: function (result) {
        if (!result.returnvalue) {
            $('#errorMessage').text("The filename already exists. Please choose another one (The files are automatically deleted every second day)");
            $('#downloadButton').css("visibility", "hidden");
        }
    }
});
});

Upvotes: 2

Related Questions