打破我的头以获得URL路由在IIS 7的托管环境:ASP.NET [英] Breaking my head to get Url Routing in IIS 7 hosting environment : ASP.NET

查看:155
本文介绍了打破我的头以获得URL路由在IIS 7的托管环境:ASP.NET的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


   我想实现ASP.NET URL路由使用 System.Web.Routing 。而这似乎很好地工作在我的本地但是当我去住我得到一个IIS(未找到文件)7的404错误。 FYI的主机使用Windows Server 2008的IIS7。

Hi I am trying to implement ASP.NET URL routing using the System.Web.Routing. And this seems to work fine on my localhost however when I go live I am getting an IIS 7's 404 error (File not found). FYI the hosting uses Windows Server 2008 IIS7.

我认为这正在处理中的路由机制存在差异。但我无法弄清楚究竟什么发生。下面是我到目前为止做出得到它的工作,并给予一定的信贷对自己它工作在本地精绝的设置和更改。

I think this is making some difference in handling the routing mechanism. But I am not able to figure out whats exactly happening. Below are the settings and changes that I've made so far to get it work and to give some credit to myself it works absolutely fine locally.

web.config设置
   

     
    

Web.Config Settings

然后我有了下面的标记一个system.webserver栏目

And then I have a system.webserver section that has the following markup

<system.webServer>
    <validation validateIntegratedModeConfiguration="false"/>
    <modules runAllManagedModulesForAllRequests="true">
      <remove name="Session" />
      <add name="Session" type="System.Web.SessionState.SessionStateModule"/>
      <add name="UrlRoutingModule"
               type="System.Web.Routing.UrlRoutingModule, 
                   System.Web.Routing, Version=3.5.0.0, 
                   Culture=neutral, 
                   PublicKeyToken=31BF3856AD364E35" />

    </modules>
    <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>   

</system.webServer>

然后在Application_Start节中,我定义了一个路线如下:

Then in the Application_Start section I have defined one route as follows:

void Application_Start(object sender, EventArgs e) 
{
    RegisterRoutes(RouteTable.Routes); 
}
void RegisterRoutes(RouteCollection routes)
{               
    routes.Add(
       "MyRoute",
          new Route("ProductDetail/{ProductId}/{ProductName}",
                new MyRouteHandler("~/ProductDetail.aspx")));
}

最后MyRouteHandler看起来如下:

Finally MyRouteHandler looks as follows:

 public IHttpHandler GetHttpHandler(RequestContext requestContext)
 {
     var display = (Page)BuildManager.CreateInstanceFromVirtualPath(
                     _virtualPath, typeof(Page));
     HttpContext.Current.Items["ProductId"] = requestContext.RouteData.Values["Product"]; 
     return display;
 }

而路由页面上,我抓住了产品ID如下:

And on the routed page I am grabbing the product ID as follows

ProductId = (int)HttpContext.Current.Items["Product"];

这是我惹的结束。而这在当地工作正常。我一直在尝试这一段时间,但至今没有成功。

And this is the end of my mess. And this works fine locally. I have been trying this for a while but didn't succeeded so far.

任何帮助将深深AP preCIATED。

ANY HELP WILL BE DEEPLY APPRECIATED.

谢谢...

推荐答案

不知道如果你能找出问题是......但是如果你还在寻找一个解决方案,那么你可以尝试以下。我不得不面对同样的局面一段时间回来,得到了它的Web配置,而您将不再需要任何路由机制使用重写规则工作。因此,首先我会鼓励你删除任何路由设置,您可能已经和code从Global.asax文件了。

Not sure if you were able to figure out what the problem was...however if you are still looking for a solution then you may try the following. I had to face the same situation some time back and got it to work using Rewrite rules in Web config for which you will not need any routing mechanism. So first I would encourage you to remove any routing setting you may have and the code from the Global.asax file too.

然后在部分中,您可以按如下添加为重写规则

Then in the section you can add as rewrite rules as follows

<rewrite>
    <rewriteMaps>
        <rewriteMap name="map1" defaultValue="(.+)"/>
    </rewriteMaps>
    <rules>
        <rule name="Rewrite rule1 for map1">
        <match url="product/(.+)/(.+)"/>
        <conditions>
            <add input="{map1:{REQUEST_URI}}" pattern="(.+)"/>
        </conditions>
        <action type="Rewrite" url="productdetail.aspx?Product={R:1}" appendQueryString="false" redirectType="Permanent"/>
        </rule>
    </rules>
  </rewrite>

如果您有了解重写机制问题,我建议你阅读的本文由Scott Guthrie的

If you have problems understanding the rewrite mechanism I would recommend that you read this article by Scott Guthrie.

我想,这应该为你工作给予了IIS 7.0或7.5的环境。

I think this should work for you given a IIS 7.0 or 7.5 environment.

这篇关于打破我的头以获得URL路由在IIS 7的托管环境:ASP.NET的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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