More control on ASP.Net MVC's Authorize; to keep AJAX requests AJAXy

前端 未结 4 1664
深忆病人
深忆病人 2020-12-28 11:17

I have some action methods behind an Authorize like:

[AcceptVerbs(HttpVerbs.Post), Authorize]
public ActionResult Create(int siteId, Comment comment) {
         


        
4条回答
  •  情歌与酒
    2020-12-28 11:59

    Instead of using the authorize attribute, I've been doing something like the following.

    public ActionResult SomeCall(string someData)
    {
        if (Request.IsAjaxRequest() == false)
        {
            // TODO: do the intended thing.
        }
        else
        {
            // This should only work with AJAX requests, so redirect
            // the user to an appropriate location.
            return RedirectToAction("Action", "Controller", new { id = ?? });
        }
    }
    

提交回复
热议问题