ASP.NET MVC, JSON & non JavaScript clients

前端 未结 2 1521
伪装坚强ぢ
伪装坚强ぢ 2021-02-10 09:19

I need to ensure that an application I am developing is accessable and also works with JavaScript turned off. I just need a pointer to assist with the following.

I had 3

2条回答
  •  南笙
    南笙 (楼主)
    2021-02-10 10:00

    You can check the IsMvcAjaxRequest property and use it inside your controller and then return a partial view (user control) or JSON result if true, or the full View if it's false.

    Something like this:

    public ActionResult List()
    {
       if (!Request.IsMvcAjaxRequest())
       {
           // Non AJAX requests see the entire ViewPage.
           return View();
       }
       else
       {
           // AJAX requests just get a trimmed down UserControl.
           return Json(...);
       }
     } 
    

    More info here: MVC AJAX Sites That Gracefully Degrade

提交回复
热议问题