简体   繁体   English

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

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

I've been trying to use url routing in asp.net 4.0 to make a clean url for my website. 我一直在尝试使用asp.net 4.0中的URL路由为我的网站创建一个干净的URL。 when I try it in localhost, it works perfectly just like what I want, but after I upload it to Windows Azure Website, I keep getting error The resource you are looking for has been removed, had its name changed, or is temporarily unavailable. 当我在localhost中尝试它时,它可以像我想要的那样完美地工作,但是在将其上载到Windows Azure网站后,我总是收到错误消息。您正在寻找的资源已被删除,名称更改或暂时不可用。

I've been trying to add code like in this URL : 我一直试图在此URL中添加代码:

and some other link with almost the same solution like the above but with no result, are there any way to implement URL Routing or any other way to make a clean url with asp.net 4.0 on Windows Azure 和具有与上述几乎相同的解决方案的其他一些链接,但没有结果,是否有任何方法可以实现URL路由,或者可以使用其他任何方法在Windows Azure上使用asp.net 4.0进行干净的url

(I don't use MVC and I deploy it on Windows Azure Website, not Cloud Service or VM) (我不使用MVC,而是将其部署在Windows Azure网站上,而不是Cloud Service或VM上)

Edit : The code that I add in Global.asax : 编辑 :我在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));
}

In WebFormRouteHandler : 在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;
    }
}

And add handler & modules in Web.config : 并在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>    

it work's perfectly when I debug it in localhost or publish it in my local IIS, but when I publish it to Windows Azure, it doesn't work 当我在localhost中调试它或在本地​​IIS中发布它时,它的工作原理是完美的,但是当我将其发布到Windows Azure时,它不起作用

Edit 2 : I've tried to publish it to Windows Azure Virtual Machine and it's working ( no error occured for now ), did anyone know any other way to make a clean url for windows azure website ( not virtual machine or cloud ), or I should move my current Website to the Virtual Machine? 编辑2 :我试图将其发布到Windows Azure虚拟机,并且它正在工作(目前没有错误),是否有人知道以其他方式为Windows Azure网站(不是虚拟机或云)制作干净的url,或者我应该将当前网站移至虚拟机吗?

After rechecking the file that I've uploaded to Windows Azure, it seems that I forgot to upload the Global.asax file, so it doesn't work. 重新检查已上传到Windows Azure的文件后,似乎忘记了上传Global.asax文件,因此它不起作用。 And after I uploaded the Global.asax file, now it works perfectly, no wonder that the routing doesn't work before. 在我上载Global.asax文件之后,现在它可以完美运行了,这也就怪不得以前的路由不起作用了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM