ASP.NET MVC, JSON & non JavaScript clients

前端 未结 2 1500
伪装坚强ぢ
伪装坚强ぢ 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

    0 讨论(0)
  • 2021-02-10 10:08

    (ref. your previous answer): You could pass a value in your Request.Form to signal that this is a browser with or without javascript enabled, and then create a controller factory that will instantiate the appropriated controller according to what you get in Request.Form; a default controller for regular requests (browsers with javascript enabled) and a "Fallback controller" that only returns full views instead. I am not sure if having twice the number of controllers is better than those if-else statements around a single controller, but I guess it's a question of personal preference.

    I bet there are simpler solutions, though..I wish this question had more exposure.

    0 讨论(0)
提交回复
热议问题