开发者

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

  1. can only be called from inside the application and not directly via route matching
  2. 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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜