开发者

ASP.NET MVC Routing strategy for static content for each View

开发者 https://www.devze.com 2022-12-16 11:40 出处:网络
I\'m wanting for every view in my system to have some static help content.The way I was thinking about doing this would be to set up a parallel structure for the static content and create a route to r

I'm wanting for every view in my system to have some static help content. The way I was thinking about doing this would be to set up a parallel structure for the static content and create a route to rewrite the URL to this. For instance:

/Controllers
/Help
  /Account
    /Login.htm
    /Create.htm
/Models
/Views
  /Account
    /Login.aspx
    /Create.aspx

...where an incoming URL for "/Account/Create/Help" would serve "/Help/Account/Create.htm". How can I add that to Global.asax:开发者_如何转开发RegisterRoutes(RouteCollection)?

Or, is it better to instead handle this with a dedicated controller and action - such as:

public class HelpController : Controller
{
    public ActionResult Help(string controller, string action)
    {
        return FileContentResult(GetContent("Help/" + controller + "/" + action));
    }
}

Or some other way?


I ended up adding a route:

        routes.MapAsyncRoute(
                    "Help",
                    "{helpController}/{helpAction}/help",
                    new { controller = "Help", action = "Help" }
                    );

which sends the help URLs to:

    public ActionResult Help(string helpController, string helpAction)
    {
        return View(helpController + "_" + helpAction);
    }

...and then named the help pages like "Account_Create.aspx". This seems like the most effective way to handle this with MVC.


I would start by getting it working with the dedicated controller as this is likely to be the least painful approach and the route should be relatively simple to write ( although you will need to make sure the /Help route is included above the default controller route as otherwise the default controller route will most likely match.

I am not sure that the asp.net runtime can serve .htm pages, so I do not know if you could even get the system working as you originally envisaged. It may be better to create a separate subdomain or virtual dir for the help content and use a html helper extension method to generate the help url from the route values available within the html helper as this way you are not adding any load to the asp.net runtime by asking it to respond to requests for static content ( and you reduce content downloaded by not requiring cookies etc ).

0

精彩评论

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