ASP.NET MVC路由策略,为每个浏览静态内容 [英] ASP.NET MVC Routing strategy for static content for each View

查看:142
本文介绍了ASP.NET MVC路由策略,为每个浏览静态内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想对每个视图在我的系统有一些静态的帮助内容。我在想这样做的方法是设置为静态内容并行结构,并创建到URL重写这一路线。例如:

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

...这里进来的URL为 /帐号/创建/帮助将成为 /Help/Account/Create.htm 。我怎么可以添加到的Global.asax:的RegisterRoutes(RouteCollection)

...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?

我最终添加路由:

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

这将帮助网址:

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

...然后命名为帮助页面,如 Account_Create.aspx 。这似乎是用MVC来处理这个最有效的方法。

...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.

我不知道该asp.net运行时可以为热媒的网页,所以我不知道,如果你甚至可以得到系统的工作,你最初的设想。这可能是最好创建的帮助内容的单独的子域或虚拟目录,并使用HTML辅助扩展方法来生成的HTML帮助为这种方式,你不添加任何负载到ASP中提供的路由值的帮助URL。通过询问其净运行时对静态内容的请求作出回应(你减少不需要饼干等下载的内容)。

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 ).

这篇关于ASP.NET MVC路由策略,为每个浏览静态内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆