繁体   English   中英

在root上托管时,Asp.net路由不起作用

[英]Asp.net Routing not works when hosted on root

我已经使用Asp.net 4.0路由技术来实现友好的url。 我在global.asax中使用了以下代码。

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

void RegisterRoutes(RouteCollection routes)
{

    string connectionstring = System.Configuration.ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
    SqlConnection sqlCon = new SqlConnection(connectionstring);
    SqlCommand sqlCmd = new SqlCommand("Sch_Sp_GetRegisterRoutes", sqlCon);
    sqlCmd.CommandType = CommandType.StoredProcedure;

    SqlDataAdapter sda = new SqlDataAdapter(sqlCmd);
    DataTable dt = new DataTable();

    try
    {
        sda.Fill(dt);
    }
    catch (Exception ex)
    {
    }
    finally
    {
        sqlCon.Close();

    }
    if (dt != null && dt.Rows.Count > 0)
    {
        foreach (DataRow dr in dt.Rows)
        {
            try
            {
                routes.MapPageRoute(dr["Menuname"].ToString().Replace(" ",string.Empty),
              dr["URL"].ToString(),
               "~/" + dr["Pagename"].ToString());
            }
            catch (Exception exx)
            {

            }

        }
    }


}

我在数据库中的URL类似于About-Us,而Pagename则类似于MyFolder / Aboutus.aspx。 在我的本地计算机上,它可以工作,但是当我在服务器(iis 7版本和Windows Server 2008)上部署它时,它显示错误'404-找不到文件或目录。 您正在寻找的资源可能已被删除,名称更改或暂时不可用,我将其称为resolveUrl(“〜/ About-Us”)。 请帮我....

只需在web.config中添加几行代码,它便对我有用。

<system.webServer>
    <modules runAllManagedModulesForAllRequests="true">
        <remove name="UrlRoutingModule-4.0" />
        <add name="UrlRoutingModule-4.0" type="System.Web.Routing.UrlRoutingModule" preCondition="" />
        <remove name="Session"/>
        <add name="Session" type="System.Web.SessionState.SessionStateModule" preCondition=""/>
    </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>

暂无
暂无

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

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