MVC3 Partial View needs a controller, but can I make this non-public?
Is it possible to create a Partial View, which has a controller, which can be called from another view using
Html.RenderAction(...)
BUT without that same controller being accessible via a URL?
So for example
public class ArticlesController : Controller
{
public ActionResult HomeList()
...
}
Gives a list of latest articles for the bottom of my web pages.
S开发者_StackOverflowo I call this from
_Layout.cshtml
However I dont want someone coming to
mysite.com/Articles/HomeList
and seeing the same list for various reasons (security, SEO, etc.)
Thanks
Edit:
I ended up using my own attribute class, thanks to Russ's help:
public class ChildActionOnly404Attribute : FilterAttribute, IAuthorizationFilter
{
void IAuthorizationFilter.OnAuthorization(AuthorizationContext filterContext)
{
if (!filterContext.IsChildAction)
{
throw new HttpException(404, "");
}
}
}
apply the ChildActionOnlyAttribute
to the action. This means that it
- can only be called from inside the application and not directly via route matching
- can be called only with the Action or RenderAction HTMLHelper extension methods
I've found it to be useful for cross-cutting concerns like menus and navigation.
精彩评论