【问题标题】:Url Routing for asp.net 4.0 Web Application on Windows Azure WebsiteWindows Azure 网站上 asp.net 4.0 Web 应用程序的 URL 路由
【发布时间】:2014-02-24 03:42:41
【问题描述】:

我一直在尝试在 asp.net 4.0 中使用 url 路由来为我的网站创建一个干净的 url。当我在 localhost 中尝试时,它就像我想要的那样完美地工作,但是在我将它上传到 Windows Azure 网站后,我不断收到错误您正在寻找的资源已被删除、其名称已更改或暂时不可用。

我一直在尝试在此 URL 中添加代码:

以及与上述解决方案几乎相同但没有结果的其他链接,是否有任何方法可以实现 URL 路由或任何其他方法在 Windows Azure 上使用 asp.net 4.0 制作干净的 url

(我不使用 MVC,我将其部署在 Windows Azure 网站上,而不是云服务或虚拟机上)

编辑:我添加的代码 在 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,或者我应该移动我当前的网站到虚拟机?

【问题讨论】:

  • 呈现链接的代码是什么,处理/配置路由的代码是什么?
  • 嗨,用我使用的代码编辑了问题

标签: c# asp.net azure url-routing


【解决方案1】:

重新检查了我上传到 Windows Azure 的文件后,我似乎忘记上传 Global.asax 文件,所以它不起作用。而且我上传了Global.asax文件后,现在完美运行,怪不得之前路由不行。

【讨论】:

    猜你喜欢
    • 2011-07-30
    • 1970-01-01
    • 1970-01-01
    • 2011-06-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多