结合actionresult和jsonresult

发布于 2024-10-19 03:22:04 字数 478 浏览 1 评论 0原文

我想这样做:

   public ActionResult Details(int id)
    {
        Object ent = new{ prop1 = 1, prop2 = 2};
        if (Request.AcceptTypes.Contains("application/json"))
         return Json(ent, JsonRequestBehavior.AllowGet);

        ViewData.Model = ent;
        return View();
    }

但想知道是否有更好的方法(和内置)来检测传入的 jsonrequest,类似于 IsAjaxRequest。我想使用相同的 url,所以最好不想处理格式扩展,如“.json”、“.html”等。

另外,我不想为 jsonrequest 和正常的 url 使用不同的 url返回视图的 Web 请求。

I want to do this:

   public ActionResult Details(int id)
    {
        Object ent = new{ prop1 = 1, prop2 = 2};
        if (Request.AcceptTypes.Contains("application/json"))
         return Json(ent, JsonRequestBehavior.AllowGet);

        ViewData.Model = ent;
        return View();
    }

But wonders if there isn't a better way (and build in) to detect an incoming jsonrequest, similar to IsAjaxRequest. I would want to use the same url, so preferably don't want to deal with format extensions, like ".json", ".html" etc.

Also I don't want to have a different url for the jsonrequest and the normal web request that returns a view.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夏有森光若流苏 2024-10-26 03:22:04

将 ActionFilterAttribute 用于 BaseController。并从 BaseController 继承所有其他控制器

[IsJsonRequest]
public abstract class BaseController : Controller
{
   public bool IsJsonRequest { get; set; }
}

The ActionFilterAttribute
public class IsJsonRequest: ActionFilterAttribute  
{  
    public override void OnActionExecuting(ActionExecutingContext filterContext)  
    { 
        var myController = filterContext.Controller as MyController;
        if (myController != null)
        {

            if (filterContext.HttpContext.Request.AcceptTypes.Contains("application/json"))
            {
                myController.IsJsonRequest = true;
            }
            else
            {
                myController.IsJsonRequest = false;
            }
        }
    }
}

public class TestController : BaseController
{
    public ActionResult Details(int id)
    {
          if (IsJsonRequest)
               return Json Data
          else
               return view
    }
}

Using ActionFilterAttribute for your BaseController. and inherit all other controllers from BaseController

[IsJsonRequest]
public abstract class BaseController : Controller
{
   public bool IsJsonRequest { get; set; }
}

The ActionFilterAttribute
public class IsJsonRequest: ActionFilterAttribute  
{  
    public override void OnActionExecuting(ActionExecutingContext filterContext)  
    { 
        var myController = filterContext.Controller as MyController;
        if (myController != null)
        {

            if (filterContext.HttpContext.Request.AcceptTypes.Contains("application/json"))
            {
                myController.IsJsonRequest = true;
            }
            else
            {
                myController.IsJsonRequest = false;
            }
        }
    }
}

public class TestController : BaseController
{
    public ActionResult Details(int id)
    {
          if (IsJsonRequest)
               return Json Data
          else
               return view
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文