【问题标题】:Asp.net Routing not works when hosted on root托管在 root 上时,Asp.net 路由不起作用
【发布时间】:2013-09-24 03:32:55
【问题描述】:

我使用了 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")。 请帮帮我....

【问题讨论】:

    标签: asp.net url-rewriting routing asp.net-routing


    【解决方案1】:

    只需在 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>
    

    【讨论】:

    • 你不需要(或不想) runAllManagedModulesForAllRequests="true"
    猜你喜欢
    • 1970-01-01
    • 2020-05-18
    • 1970-01-01
    • 2011-03-04
    • 2021-11-04
    • 1970-01-01
    • 2020-07-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多