ASP.NET MVC

Prevent direct access to a controller action returning a partial view

Add the [ChildActionOnly] attribute to the action method or class

[ChildActionOnly]
public ActionResult Blog_TagsBox()
{
    var tags = _blogPostService.GetAllTags().Split(',');
    return PartialView(tags);
}

Shorthand route mapping

An extension method to enable shorthand mapping of routes

// extension method
public static Route MapRoute(this RouteCollection routes, string url, object defaults, object constraints = null)
{
    return routes.MapRoute(
        name: null,
        url: url,
        defaults: defaults,
        constraints: constraints,
        namespaces: new[] { "MyProject.Web.Controllers" }
    );
}

//example usage
routes.MapRoute(
    url: "our-people",
    defaults: new { controller = "Staff", action = "Index" }
);
Sponsored by