Asp.net MVC Ajax调用没有调用控制器方法

bri*_*342 5 c# asp.net ajax asp.net-mvc jquery

我正在制作ASP.net MVC应用程序我希望如此,当用户点击链接时,它会执行ajax调用,将数据发送到控制器,然后将其他数据返回给视图.

这是我想在我的控制器中调用的方法:

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

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

            return Json(new { byteImage }, JsonRequestBehavior.AllowGet);
        }
Run Code Online (Sandbox Code Playgroud)

这里是控制器位置:

08983ClassLibrary\EpostASP\Controllers\CustomerController.cs
Run Code Online (Sandbox Code Playgroud)

这是我的Ajax调用:

$.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();
            }
        });
Run Code Online (Sandbox Code Playgroud)

当我在代码中放置断点时,我可以看到它击中了ajax调用,然后踩到它从未到达控制器并且不会给出任何错误.有任何想法吗?

bra*_*der 12

主要问题在这里 -

url: "~/Controllers/CustomerController/GetImage/",
Run Code Online (Sandbox Code Playgroud)

您看,~是服务器端文字,换句话说,当您在ASP.net服务器端路径中使用它时,它将被当前服务器应用程序文件夹位置替换.这是传统的ASP.Net方式.这条线有2个错误 -

  1. 这个网址永远不会有用.因为它在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`
    
    Run Code Online (Sandbox Code Playgroud)

请注意,我没有写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();
        }
    });
Run Code Online (Sandbox Code Playgroud)

但为了安全起见,请确保使用 -

[HttpPost]
public JsonResult GetImage(string url)
{
}
Run Code Online (Sandbox Code Playgroud)

更新:maproute(在评论中请求)将适用于任何这些路线.但也可以使用不同的路由配置.Route配置非常灵活,只需按照它的工作方式设置路由即可. -

    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);
    }
Run Code Online (Sandbox Code Playgroud)