繁体   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