如何实现URL使用Windows Azure重写? [英] How to implement URL rewriting with Windows Azure?

查看:199
本文介绍了如何实现URL使用Windows Azure重写?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有关于Windows Azure中承载的ASP.NET / C#的网站。该网站是一个基于pdictions $ P $社交网站与prediction总结的主要页面上的饲料。如果你点击了一个总结,你重定向到使用一个简单的查询字符串是prediction的详细信息页面。

I have an ASP.NET / C# website that's hosted on Windows Azure. The site is a predictions-based social site with a feed of prediction summaries on the main page. If you click on a summary, you're redirected to the details page for that prediction using a simple QueryString.

例如:

<一个href=\"http://www.i$p$pdikt.com/details.aspx?id=14\">http://www.i$p$pdikt.com/details.aspx?id=14

这个特殊的prediction有权的帕丽斯·希尔顿将赢得诺贝尔和平奖的,所以我想要做的是什么执行URL重写为我的网站在天青如下:

This particular prediction is entitled "Paris Hilton will win the Nobel Peace Prize" so what I'd like to do is implement URL rewriting for my site on Azure as follows:

<一个href=\"http://www.i$p$pdikt.com/$p$pdictions/14/paris-hilton-will-win-the-nobel-peace-prize\">http://www.i$p$pdikt.com/$p$pdictions/14/paris-hilton-will-win-the-nobel-peace-prize

什么是做一些这方面的策略和最佳实践?而且可以有人点我一个很好的Azure的具体两篇文章。

What are some strategies and best practices for doing this? And can someone point me to a good Azure-specific article or two.

复姓标题(巴黎 - 希尔顿喇嘛喇嘛)是真的只是为了让更多的URL人类可读的;我不想像依靠它在所有的页面加载的条款。事实上,我可能会允许重复的标题,因为我将依托于URL中的prediction ID。

The hyphenated title ("paris-hilton-bla-bla") is really just to make the URL more human readable; I don't envision relying on it at all in terms of loading pages. In fact, I'd probably allow duplicate titles since I'll be relying on the prediction ID in the URL.

编辑:

忘了提,我们的不可以基于MVC。我们来到了瓦/我们自己的架构,使用PageMethods和webMethods以JSON返回给客户端。我们依靠ASP.NET AJAX做所有的JSON序列化的,并且几乎所有的UI的动态构建使用jQuery在客户端上。

Forgot to mention that we are NOT based on MVC. We came up w/ our own architecture that uses PageMethods and WebMethods to return JSON to the client. We rely on ASP.NET AJAX to do all of the JSON serialization, and almost all of our UI is built dynamically on the client using jQuery.

编辑:SOLUTION

想我会分享我的解决方案,现在,我有事情和运行。

Thought I'd share my solution now that I have things up and running.

我做了一个新的类如下(从某处逐字复制):

I made a new class as follows (copied verbatim from somewhere):

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();
   }
}

我添加下面的方法Global.asax中。实际的方法很多,更长的(它覆盖在网站的每个页面)。你会看到,我支持许多不同的方式调用predictions页面:一个ID,一个ID +标题等(页的...... FB的版本是的Facebook应用程序版本我的网站使用不同的母版。)

I added the following method to Global.asax. The actual method is MUCH, much longer (it covers every page in the site). You'll see that I support calling the predictions page in lots of different ways: with an id, with an id + title, etc. (The "...fb" versions of pages are for the Facebook app version of my site which use a different 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));
   }

...和这个方法是从的Application_Start称为()

...and this method is called from Application_Start()

  void Application_Start(object sender, EventArgs e)
  {
     RegisterRoutes(RouteTable.Routes);
  }

然后我说在system.webServer块以下到web.config中:

Then I added the following to web.config in the system.webServer block:

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

我也不得不排除验证虚拟predictions目录(因为几乎所有的零件我们网站的可访问我的非auth用户):

I also had to exclude the virtual "predictions" directory from authentication (because almost all parts of our site are accessible my non-auth users):

<!-- Url routing -->
<location path="predictions">
   <system.web>
      <authorization>
         <allow users="*" />
      </authorization>
   </system.web>
</location>

最后,我加载页面时,不再依靠查询字符串字符串参数,所以我不得不写一些新的辅助方法。这里有一个从提取新的路由URL数值(我会清理这件事只能有一个单一的回归。):

Finally, I no longer rely on QueryString string parameters when loading pages, so I had to write some new helper methods. Here's one that extracts a numerical value from the new routing URL (I'll be cleaning this up to only have a single '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;
  }

现在,当我需要读取一个路由值(如prediction ID),我做到以下几点:

Now when I need to read a routing value (like a prediction ID), I do the following:

  long _predictionId = System.Convert.ToInt64(WebAppUtils.GetRouteDataValueAsNumber(Request, "id"));

伟大工程!现在我的网站感觉与友好和自我记录的URL的MVC应用程序。

Works great! Now my site feels like an MVC app with friendly and self-documenting URLs.

哦,最后一件事,你还需要启用HTTP重定向如下:

Oh, last thing, you also need to enable HTTP Redirection as follows:

开始=>控制面板=>程序=>打开Windows功能=> Internet信息服务=>万维网服务=>常见HTTP功能=>(选择复选框),HTTP重定向。

Start => Control Panel => Program => Turns Windows Features On => Internet Information Services => World Wide Web Services => Common HTTP Features => (select checkbox for) HTTP Redirection.

推荐答案

实现,这将是使用 System.Web.Routing 组件的编程方法最简单的方法。

The easiest way to implement this would be a programmatic approach using the System.Web.Routing assembly.

这基本上是你的的web.config ,作品被包括 UrlRoutingModule 和定义模式,解决目标页面基于匹配的路线。如果你熟悉ASP.NET MVC,那么你已经使用过该路由策略,但MVC是没有必要使用路由。

This basically works by including the UrlRoutingModule in your web.config, and defining patterns that resolve the target page based on matching routes. If you are familiar with ASP.NET MVC, then you have used this routing strategy before, but MVC is not necessary to use Routing.

<一个href=\"http://weblogs.asp.net/scottgu/archive/2007/12/03/asp-net-mvc-framework-part-2-url-routing.aspx\">Scott顾在URL路由的MVC - *注意,本文介绍了在路由ASP.NET MVC应用程序的情况下,但是,同样的方法将不管您是否正在使用MVC

ASP.NET路由...再见URL重写,由克里斯·卡瓦纳 - 一个解释性的文章

ASP.NET Routing... Goodbye URL rewriting, by Chris Cavanagh - An explanatory article

探索System.Web.Routing,由Justin Etheredge - 为例,说明​​如何使用独立的MVC架构的路由

Exploring System.Web.Routing, by Justin Etheredge - A case study explaining how to use routing independently of the MVC architecture

如果你采用这种方式,它并不真正的问题,您使用的是Windows Azure上。然而,我发现迈克尔·肯尼迪的一篇名为 ASP.NET路由在Windows Azure中使用的WebForms ,解释如何轻松部署在Windows Azure上这样的解决方案。文章甚至有一个<一个href=\"http://www.michaelckennedy.net/blog/ct.ashx?id=210f6623-b67e-4b6b-835e-1576bc2cfb3c&url=http%3a%2f%2fwww.michaelckennedy.com%2fSamples%2fAzureRoutingSample.zip\">sample项目下载。

If you take this approach, it doesn't really matter that you are using Windows Azure. However, I found an article by Michael Kennedy called ASP.NET Routing in Windows Azure Using WebForms, explaining how to easily deploy such a solution on Windows Azure. The article even has a sample project for download.

这篇关于如何实现URL使用Windows Azure重写?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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