难道我可以prevent一定PartialViews如果从直接要求被服务? [英] Is it i possible to prevent certain PartialViews from being served if requested directly?
问题描述
我的工作有一种路由使部分景色行动的网站。很多这些局部视图组件共同构成一个完整的页面。
I'm working on a site that has routes to actions which render Partial Views. A lot of these partial views are components which together make up a complete page.
例如我工作有一个文本框,标签列表,表中的搜索页面上。
For instance on a search page I'm working on has a text box, a list of tabs, and a Table.
的这些SEACH可以用类似的URL被访问
Seach of these can be accessed with a URL similar to
/Search/SearchPanel
/Search/Tabs/{SearchTerm}
/Search/ResultsTable/SearchTerm?tab=[currently selected tab]
和这些都渲染上了我的RenderPartial索引页。
and these are all rendered on with a RenderPartial on my Index page.
在页面加载时,将我想要的方式显示这些组件。但此刻没有什么直接去到URL阻止用户
When the page loads, it will display each of these components the way I want it. But at the moment there's nothing stopping a user from going directly to the url
/Search/Tabs
渲染仅是无意义的页面上的元素的其余部分的上下文之外的标签控制
to render only a tab control which is meaningless outside the context of the rest of the elements on the page.
有没有办法对我来说,prevent呢?
Is there a way for me to prevent this?
推荐答案
您是否尝试过打你的控制器方法私人
?
Have you tried marking your Controller method as private
?
private PartialViewResult MyPartialResultMethod()
这应该允许您从您的调用code范围内建立起自己的网页,并禁止任何公开的访问,如通过URL。
This should allow you to call it from within your code to build up your pages and disallow any public access such as through a URl.
我测试这个现在为了以防万一我的答案是正确的,所以我会更新,因为我测试了。
I'm testing this now to make doubly sure my answer is correct so I'll update as I test.
在你的标签例如,你可以简单地通过使用标签的第二控制器方法,它是私有的限制访问。
In your tabs example you could simply restrict access by using a second controller method for Tabs that's private.
所以你得东西,看起来像:
So you'd have something that looks like:
public ActionResult Tabs(string searchTerm) // When a search term is passed.
和
private ActionResult Tabs() // When no search term is passed.
这篇关于难道我可以prevent一定PartialViews如果从直接要求被服务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!