Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/30.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/ant/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
为什么在ASP.Net核心中不推荐IsAjaxRequest_Asp.net_Asp.net Core - Fatal编程技术网

为什么在ASP.Net核心中不推荐IsAjaxRequest

为什么在ASP.Net核心中不推荐IsAjaxRequest,asp.net,asp.net-core,Asp.net,Asp.net Core,有没有人告诉我为什么ASP.NETCore中不推荐IsAjaxRequest 或者是否有其他功能 我在我的项目中使用了这个函数allot,现在我想升级到ASP.Net Core if (Request.IsAjaxRequest()) { return PartialView("index2", VMJob); } return View(VMModel); 有一个解决办法: 但是我想知道ASP.Net c

有没有人告诉我为什么ASP.NETCore中不推荐IsAjaxRequest 或者是否有其他功能

我在我的项目中使用了这个函数allot,现在我想升级到ASP.Net Core

        if (Request.IsAjaxRequest())
        {
            return PartialView("index2", VMJob);
        }
        return View(VMModel);
有一个解决办法:


但是我想知道ASP.Net core中是否有内置函数有一种方法可以限制您的操作方法只响应ajax请求:

public class AjaxOnlyAttribute : ActionMethodSelectorAttribute
{
    public override bool IsValidForRequest(RouteContext routeContext, 
        ActionDescriptor action)
    {
        HttpRequest request = routeContext.HttpContext.Request;
        if (request == null)
        {
            return false;
        }

        if (request.Headers != null)
        {
            return request.Headers["X-Requested-With"] == "XMLHttpRequest";
        }

        return false;
    }
}
然后在控制器中:

[Route("api/[controller]")]
public class UsersController : Controller
{
    [HttpGet]
    [AjaxOnly]
    public IActionResult GetAll()
    {
        return Ok(new[]
        {
            new UserDto{Id = 1, Name = "John"},
            new UserDto{Id = 2, Name = "Snow"}
        });
    }

    private class UserDto
    {
        public int Id { get; set; }
        public string Name { get; set; }
    }
}

通过这种方式,您可以创建两个单独的操作方法,一个用于ajax请求,另一个用于标准的完整页面重新加载。

不再需要它,因为将ASP.NET重写为ASP.NET核心后,WebAPI和MVC现在都是同一个API。另外,在一个动作中混合使用MVC和WebAPI也是一种不好的做法,因为MVC使用Controller+action+Http谓词,而WebAPI只使用Controller+Http谓词(动作名称不用于标识WebAPI路由),即MVC中的
POST/Home/User/1/Edit
POST/PUT/api/Home/User/
相比,前者不是格式良好的REST uri/route。还要记住,ASP.NET MVC是在WebAPI出现之前创建的。如何以ASP.NET coreClean的方式处理上述ajax请求:创建两个端点,一个用于ajax,一个用于MVC。肮脏的方式,使用链接文章你有一个样本代码后,它作为答案,让我检查它?那对我有用还是对我有用not@TsengWebAPI并不是需要它的唯一原因。基于SPA的网络应用比以往任何时候都更需要这一点。只要使用MVC5Well的原始代码重新创建扩展,其思想是,当您使用AjaxOnly属性标记您的操作方法,并且请求不是ajax时,例如,您直接在浏览器中键入,这不是ajax,操作方法将不会被解析。此外,它还向您展示了一种解决缺少
IsAjaxRequest
方法的方法,以及如何检查该方法是否为ajax。