Asp.net MVC Ajax non-calling method call

I am creating an ASP.net MVC application. And I would like for him, when the user clicks on the link, makes an ajax call, passing the data to the controller, and then returns the other data back to the view.

This is the method I would like to call in the controller:

public JsonResult GetImage(string url)
        {
            Image image = Image.FromFile(url, true);

            byte[] byteImage = converter.ImageToBytes(image);

            return Json(new { byteImage }, JsonRequestBehavior.AllowGet);
        }

And here is the location of the controllers:

08983ClassLibrary\EpostASP\Controllers\CustomerController.cs

This is my Ajax Call:

$.ajax({
            url: "~/Controllers/CustomerController/GetImage/",
            type: 'POST',
            contentType: 'application/json',
            data: "url="+url,
            success: function (image) {
                document.getElementById("image").src = "data:image/png;base64," + image;
                showImage();
            }
        });

When I put my breakpoints in the code, I see that it hits the ajax call and then steps over it, it never reaches the controller and gives no errors. Any ideas?

+5
source share
8 answers

The main problem here is

url: "~/Controllers/CustomerController/GetImage/",

, ~ , , ASP.net, . ASP.Net. 2 -

  1. URL . JS , , ASP.net , . , ASP.net , . - , 2 -

  2. ASP.net MVC, . MVC - . ASP.net (.aspx,.ascx) . MVC . , ( Global.asax), URL- . MVC -

    <host>/{controller}/action

    -

    'localhost/Home/Index'
    

, HomeController, Controller.

, , , ( , ) -

 $.ajax({
        url: "Customer/GetImage",
        type: 'POST',
        contentType: 'application/json',
        data: "url="+url,
        success: function (image) {
            document.getElementById("image").src = "data:image/png;base64," + image;
            showImage();
        }
    });

, , -

[HttpPost]
public JsonResult GetImage(string url)
{
}

UPDATE: maproute ( ) . . , , . -

    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.MapRoute(
            "...",                                              // Route name
            "{controller}/{action}/{id}",                           // URL with parameters
            new { controller = "Home", action = "Index", id = "" }  // Parameter defaults
        );

        routes.MapRoute(
            "...",                                              // Route name
            "{controller}/{action}",                           // URL with parameters
            new { controller = "Home", action = "Index", id = "" }  // Parameter defaults
        );

        routes.MapRoute(
            "...",                                              // Route name
            "{controller}/{action}/{id}",                           // URL with parameters
            new { controller = "Customer", action = "GetImage", id = "" }  // Parameter defaults
        );
        routes.MapRoute(
            "...",                                              // Route name
            "Customer/GetImage/{id}",                           // URL with parameters
            new { controller = "Customer", action = "GetImage", id = "" }  // Parameter defaults
        );
        ..... //all of these mentioned route will land on the same url 
    }

    protected void Application_Start()
    {
        RegisterRoutes(RouteTable.Routes);
    }
+12

ajax script , POST.

$.ajax({type: 'POST',

POST?

[HttpPost]
public JsonResult GetImage(string url)
{
}

ajax

$.ajax({
    data: {
        "url": url 
        },
+4

, " " → "" . , , , .. , .

+3

-api... "Get" GetImages, [HttpPost]

0

, fiddler - , MVC.

, , . ( Yorro) , 404 , json- , .

0
$.ajax({
     url: '@Url.Action("GetImage", "Customer")',
    type: 'POST',
   contentType: "application/json; charset=utf-8",
    data: "url="+url,
    success: function (image) {
        document.getElementById("image").src = "data:image/png;base64," + image;
        showImage();
    }
});
0

IE, , ...

$. Ajax ({ : , ...

0

This will work for some time or will not work for some time, so do not mention URLs such as static "~ / Controllers / CustomerController / GetImage /" , please follow the dynamic method

Thus, you change the code fragment, for example below

$.ajax({
        url: "@Url.Action("GetImage", "CustomerController")",
        type: 'POST',
        contentType: 'application/json',
        data: "url="+url,
        success: function (image) {
            document.getElementById("image").src = "data:image/png;base64," + image;
            showImage();
        }
    });

hope this code solves your problem !!

0
source

Source: https://habr.com/ru/post/1538813/


All Articles