我有一个支持多个站点的应用程序。该站点将根据 url 确定。
例如
http://myapp/site/abc123/...
和
http://myapp/site/xyz123/...
站点代码将驱动很多功能例如主题、可用模块等...
问题:
1-)我需要验证站点代码是否有效,如果无效,它应该将用户引导至信息页面。我正在考虑使用 IRouteConstraint,是这个合适吗?还有其他/更好的选择吗?
2-)这种方法有什么问题吗(使用 url 来识别站点)?有更好的方法吗?
解决方案
我最终创建了一个自定义 ActionFilter 并检查 OnActionExecuting 事件中的站点代码。这似乎比 IRouteConstraint 效果更好并且更适合。
I have an application that will support multiple sites. The site will be determined based on the url.
For example
http://myapp/site/abc123/...
and
http://myapp/site/xyz123/...
The site code will drive a lot of the functionality for example themes, available modules, etc...
Questions:
1-)I need to validate the site code is valid and if it isn't, it should direct the user to an info page. I was looking at using IRouteConstraint, is this appropriate? Are there other/better options?
2-)Any gotchas with this approach (using url to identify site)? Is there are better approach?
Solution
I ended up creating a Custom ActionFilter and check the sitecode in the OnActionExecuting event. That seems to work well and fit better than the IRouteConstraint.
发布评论
评论(2)
我实现的系统使用 URL 来识别单个站点内的唯一页面内容,并且路由过程非常简单。话虽这么说,您可能需要考虑在 MVC 应用程序中使用区域。使用 Areas,您的网站可以有多个部分,这些部分都有自己的 MVC 结构,可以半独立运行。
本质上,您将拥有一个基本路由定义,其中列出了一些默认值,然后其余“站点”将定义自己的路由,指向单独位置的控制器和视图。设置非常简单,您只需确保使用 ASP.NET MVC 2.0 版本即可。这是关于 ASP.NET MVC 区域和路由。在 MVC 2.0 支持的当前模型中,每个区域都将有一个 Web 项目,但这不一定是必需的。 Phil Haacked 有一些ASP.NET MVC 单一项目区域<的代码/a> 如果您正在寻找该技术的另一个示例,尽管您个人可能会从第一篇文章中受益更多。
只要您定义了具有明确且可衡量的约束的良好路线,您在布置您所描述的网站时就不会有太多麻烦。
The system I have implemented uses Urls to identify unique page content within a single site and the routing process is pretty straightforward. That being said, you may want to consider making use of Areas in your MVC application. With Areas you can have multiple sections to your website that all have their own MVC structure which can run semi-independently.
Essentially, you will have one base routing definition that lays out some defaults and then the rest of the "sites" will define their own routes pointing to controllers and views in a separate location. It's pretty easy to set up, you'll just need to make sure you're using version 2.0 of ASP.NET MVC. Here's a decent looking tutorial on ASP.NET MVC Areas and Routes. In the current model which MVC 2.0 supports you'll have a single Web project for each area, but that is not necessarily a requirement. Phil Haacked has some code for ASP.NET MVC Single Project Areas if you're looking for another example of the technique, although you, personally, will probably benefit more from the first article.
So long as you define good routes that have clear and measurable constraints, you shouldn't have too much trouble laying out the website you've described.
我最终创建了一个自定义 ActionFilter 并检查 OnActionExecuting 事件中的站点代码。这似乎比 IRouteConstraint 效果更好并且更适合。
I ended up creating a Custom ActionFilter and check the sitecode in the OnActionExecuting event. That seems to work well and fit better than the IRouteConstraint.