asp.net mvc partials do they know if the are run stand alone or as part of a page

南楼画角 提交于 2019-12-11 13:51:52

问题


Does anyone know, if a partial view (PartialViewResult) in an ascx file - knows if it is running stand alone - called directly from the browser or as part of a page.

I would like to treat these cases differently.

Edit:
Case 1:
In the page case it could be that a page has a few partials that are loaded using ajax while the page is being built. The partials might be called again using ajax according to the users actions. In this case I consider them as controls on a page.

Case 2:In the stand alone case the partial could be called as part of a test directly from the browser. In this case you only see the partial part in the browser.

Case 3:In the third case the partial could be called as part of an iframe within a google chrome extension (for example). In this case you can see the partial in a page that might not have been built in your web application.

Hope this makes sense.

Thanks in advance of for those whom it concerns - happy new year.


回答1:


You could distinguish between normal requests and ajax requests (Request.IsAjaxRequest()) and child actions (ControllerContext.IsChildAction - rendered with Html.Action).

As an alternative the controller action returning the partial view could set some model property indicating that the partial was rendered via this action instead of being directly included in a view with Html.RenderPartial for example.



来源:https://stackoverflow.com/questions/4562727/asp-net-mvc-partials-do-they-know-if-the-are-run-stand-alone-or-as-part-of-a-pag

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!