簡體   English   中英

Windows Azure網站上的asp.net 4.0 Web應用程序的URL路由

[英]Url Routing for asp.net 4.0 Web Application on Windows Azure Website

我一直在嘗試使用asp.net 4.0中的URL路由為我的網站創建一個干凈的URL。 當我在localhost中嘗試它時,它可以像我想要的那樣完美地工作,但是在將其上載到Windows Azure網站后,我總是收到錯誤消息。您正在尋找的資源已被刪除,名稱更改或暫時不可用。

我一直試圖在此URL中添加代碼:

和具有與上述幾乎相同的解決方案的其他一些鏈接,但沒有結果,是否有任何方法可以實現URL路由,或者可以使用其他任何方法在Windows Azure上使用asp.net 4.0進行干凈的url

(我不使用MVC,而是將其部署在Windows Azure網站上,而不是Cloud Service或VM上)

編輯 :我在Global.asax中添加的代碼:

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

public static void RegisterRoutes(RouteCollection routes)
{
        var routeHandlerBrands = new WebFormRouteHandler<Page>("~/brands_book.aspx");
        var routeHandlerCategories = new WebFormRouteHandler<Page>("~/categories_fs.aspx");

        RouteTable.Routes.Add(new Route("Catalog/Categories", routeHandlerCategories));
        RouteTable.Routes.Add(new Route("Catalog/Brands", routeHandlerBrands));
}

在WebFormRouteHandler中:

public interface IRoutablePage
{
    RequestContext RequestContext { set; }
}
public class WebFormRouteHandler<T> : IRouteHandler where T : IHttpHandler, new()
{
    public WebFormRouteHandler(string virtualPath)
        : this(virtualPath, true)
    {
    }

    public WebFormRouteHandler(string virtualPath, bool checkPhysicalUrlAccess)
    {
        this.VirtualPath = virtualPath;
        this.CheckPhysicalUrlAccess = checkPhysicalUrlAccess;
    }

    public string VirtualPath { get; private set; }

    public bool CheckPhysicalUrlAccess { get; set; }

    public IHttpHandler GetHttpHandler(RequestContext requestContext)
    {
        if (this.CheckPhysicalUrlAccess
          && !UrlAuthorizationModule.CheckUrlAccessForPrincipal(this.VirtualPath
                  , requestContext.HttpContext.User
                  , requestContext.HttpContext.Request.HttpMethod))
            throw new SecurityException();

        var page = BuildManager
          .CreateInstanceFromVirtualPath(this.VirtualPath
            , typeof(Page)) as IHttpHandler;

        if (page != null)
        {
            var routablePage = page as IRoutablePage;
            if (routablePage != null)
                routablePage.RequestContext = requestContext;
        }
        return page;
    }
}

並在Web.config中添加處理程序和模塊:

<system.webServer>
<modules>
  <remove name="UrlRoutingModule-4.0" />
  <add name="UrlRoutingModule-4.0" type="System.Web.Routing.UrlRoutingModule" preCondition="" />
</modules>
<handlers>
  <remove name="ExtensionlessUrlHandler-Integrated-4.0" />
  <add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="*" type="System.Web.Handlers.TransferRequestHandler" resourceType="Unspecified" requireAccess="Script" preCondition="integratedMode,runtimeVersionv4.0" />
  <add name="UrlRoutingHandler" preCondition="integratedMode" verb="*" path="UrlRouting.axd" type="Route.RoutingHandler, Route"/>
</handlers>    

當我在localhost中調試它或在本地​​IIS中發布它時,它的工作原理是完美的,但是當我將其發布到Windows Azure時,它不起作用

編輯2 :我試圖將其發布到Windows Azure虛擬機,並且它正在工作(目前沒有錯誤),是否有人知道以其他方式為Windows Azure網站(不是虛擬機或雲)制作干凈的url,或者我應該將當前網站移至虛擬機嗎?

重新檢查已上傳到Windows Azure的文件后,似乎忘記了上傳Global.asax文件,因此它不起作用。 在我上載Global.asax文件之后,現在它可以完美運行了,這也就怪不得以前的路由不起作用了。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM