Arm*_*nco 9 c# asp.net url-rewriting azure query-string
我有一个在Windows Azure上托管的ASP.NET/C#网站.该网站是一个基于预测的社交网站,在主页上提供了预测摘要.如果单击摘要,则会使用简单的QueryString将您重定向到该预测的详细信息页面.
例如:
http://www.ipredikt.com/details.aspx?id=14
这个特别的预测名为"帕丽斯·希尔顿将赢得诺贝尔和平奖",所以我想做的是在Azure上为我的网站实施URL重写,如下所示:
http://www.ipredikt.com/predictions/14/paris-hilton-will-win-the-nobel-peace-prize
这样做的策略和最佳实践是什么?并且有人可以指向我一篇优秀的Azure特定文章.
带连字符的标题("paris-hilton-bla-bla")实际上只是为了让URL更具人性化; 在加载页面方面,我根本没想到完全依赖它.事实上,我可能会允许重复的标题,因为我将依赖URL中的预测ID.
编辑:
忘了提一下我们不是基于MVC.我们提出了自己的架构,它使用PageMethods和WebMethods将JSON返回给客户端.我们依靠ASP.NET AJAX来完成所有JSON序列化,几乎所有的UI都是使用jQuery在客户端上动态构建的.
编辑:解决方案
以为我现在分享我的解决方案,我已经开始运行了.
我按如下方式创建了一个新类(从某处逐字复制):
public class WebFormRouteHandler<T> : IRouteHandler where T : IHttpHandler, new()
{
public string VirtualPath { get; set; }
public WebFormRouteHandler(string virtualPath)
{
this.VirtualPath = virtualPath;
}
public IHttpHandler GetHttpHandler(RequestContext requestContext)
{
return (VirtualPath != null)
? (IHttpHandler)BuildManager.CreateInstanceFromVirtualPath(VirtualPath, typeof(T))
: new T();
}
}
Run Code Online (Sandbox Code Playgroud)
我将以下方法添加到Global.asax.实际的方法很长,更长(它涵盖了网站中的每一页).你会看到我支持以多种不同的方式调用预测页面:带有id,带有id + title等等.("... fb"版本的页面是针对我网站的Facebook应用版本的使用不同的MasterPage.)
public static void RegisterRoutes(RouteCollection routes)
{
// Details : 'predictions' Page
var routeHandlerDetails = new WebFormRouteHandler<Page>("~/details.aspx");
var routeHandlerDetailsFb = new WebFormRouteHandler<Page>("~/detailsfb.aspx");
routes.Add(new Route("predictions/{id}", routeHandlerDetails));
routes.Add(new Route("predictions/{id}/{title}", routeHandlerDetails));
routes.Add(new Route("fb/predictions/{id}", routeHandlerDetailsFb));
routes.Add(new Route("fb/predictions/{id}/{title}", routeHandlerDetailsFb));
}
Run Code Online (Sandbox Code Playgroud)
...从Application_Start()调用此方法
void Application_Start(object sender, EventArgs e)
{
RegisterRoutes(RouteTable.Routes);
}
Run Code Online (Sandbox Code Playgroud)
然后我将以下内容添加到system.webServer块中的web.config:
<!-- Added for URL Routing -->
<modules runAllManagedModulesForAllRequests="true">
<add name="UrlRoutingModule"
type="System.Web.Routing.UrlRoutingModule, System.Web.Routing, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
</modules>
<!-- Added for URL Routing -->
<handlers>
<add name="UrlRoutingHandler"
preCondition="integratedMode"
verb="*"
path="UrlRouting.axd"
type="System.Web.HttpForbiddenHandler, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
</handlers>
Run Code Online (Sandbox Code Playgroud)
我还必须从身份验证中排除虚拟"预测"目录(因为我的非身份验证用户几乎可以访问我们网站的所有部分):
<!-- Url routing -->
<location path="predictions">
<system.web>
<authorization>
<allow users="*" />
</authorization>
</system.web>
</location>
Run Code Online (Sandbox Code Playgroud)
最后,我在加载页面时不再依赖QueryString字符串参数,因此我不得不编写一些新的辅助方法.这是从新路由URL中提取数值的一个(我将清理它只有一个'return'.):
public static int GetRouteDataValueAsNumber(HttpRequest request, string propertyName)
{
if ((request == null) ||
(request.RequestContext == null) ||
(request.RequestContext.RouteData == null) ||
(request.RequestContext.RouteData.Values[propertyName] == null))
{
return -1;
}
try
{
return System.Convert.ToInt32(request.RequestContext.RouteData.Values[propertyName]);
}
catch
{
}
return -1;
}
Run Code Online (Sandbox Code Playgroud)
现在当我需要读取路由值(如预测ID)时,我会执行以下操作:
long _predictionId = System.Convert.ToInt64(WebAppUtils.GetRouteDataValueAsNumber(Request, "id"));
Run Code Online (Sandbox Code Playgroud)
效果很好!现在,我的网站感觉就像一个MVC应用程序,具有友好和自我记录的URL.
哦,最后,您还需要启用HTTP重定向,如下所示:
开始=>控制面板=>程序=>打开Windows功能=> Internet信息服务=>万维网服务=>常见HTTP功能=>(选择复选框)HTTP重定向.
sma*_*man 12
实现这一点的最简单方法是使用System.Web.Routing程序集的编程方法.
这基本上可以通过包含UrlRoutingModule您的web.config,以及根据匹配路由定义解析目标页面的模式来实现.如果您熟悉ASP.NET MVC,那么之前已经使用过此路由策略,但MVC不是必须使用Routing.
Scott Gu关于MVC的URL路由 - *请注意,本文解释了在ASP.NET MVC应用程序的上下文中的路由,但是,无论您是否使用MVC,相同的方法都可以工作
ASP.NET Routing ... Goodbye URL重写,作者:Chris Cavanagh - 一篇解释性文章
探索由Justin Etheredge撰写的System.Web.Routing - 一个案例研究,解释如何独立于MVC架构使用路由
如果采用这种方法,那么使用Windows Azure并不重要.但是,我发现Michael Kennedy的一篇文章称使用WebForms在Windows Azure中使用ASP.NET路由,解释了如何在Windows Azure上轻松部署此类解决方案.该文章甚至有一个示例项目供下载.
| 归档时间: |
|
| 查看次数: |
7534 次 |
| 最近记录: |