繁体   English   中英

URL映射-加载第一页后将Default.aspx更改为虚拟页

[英]URL Mapping - Change Default.aspx to virtual page after first page load

我想要虚拟页面,而不是像Default.aspx一样:
www.mydomain.com/Default.aspx > www.mydomain.com/Virual-Page
在浏览器中打开我的网址后。

我如何更改web.config以实现该目标?

web.config中的以下代码不起作用。

<urlMappings enabled="true">
  <add url="~/Virtual-Page" mappedUrl="~/Default.aspx"/>
</urlMappings>

您需要此软件包:有关如何实现ASP.NET友好URL的信息 ,请参见Hanselman的博客

将此添加到您的global.asax文件中

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

如下所示,在Global.asax文件的顶部添加引用:

using System.Web.Routing;

这会自动重写您的URL。

如果要保留default.aspx,请使用URL映射:

<urlMappings enabled="true">
    <add url="~/Virtual-Page" mappedUrl="~/default.aspx" />
</urlMappings>

添加重写规则以使用URL重写重定向您的基本URL

<system.webServer>
  <rewrite>
    <rules>
      <rule name="Rewrite to Virtual-Page">
        <match url="(.*)" />
        <action type="Redirect" url="~/Virtual-Page" redirectType="Permanent" />
      </rule>
    </rules>
    </rewrite>
  </system.webServer>

如果您不能使用URL重写,请在global.asax开始请求中添加response.redirect:

void Application_BeginRequest(object sender, EventArgs e)
    if (!HttpContext.Current.Request.Url.ToString().ToLower().Contains("http://localhost/Virtual-Page"))
    {
                HttpContext.Current.Response.Redirect(HttpContext.Current.Request.Url.ToString().ToLower().Replace("http://localhost/", "http://localhost/Virtual-Page"));
    }

暂无
暂无

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

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