码迷,mamicode.com
首页 > 其他好文 > 详细

页面伪静态

时间:2015-07-02 15:36:52      阅读:103      评论:0      收藏:0      [点我收藏+]

标签:

mvc可以直接配置路由,虽然aspx已经是过去式,但是学习一下也是有必要的。

1、直接在global.ashax中Application_BeginRequest方法中讲url重写

      protected void Application_BeginRequest(object sender, EventArgs e)
        {
            var path = Request.AppRelativeCurrentExecutionFilePath;
            Regex regex = new Regex(@"~/default-(\d+)");
            if (regex.IsMatch(path))
            {
                var realPath = regex.Replace(path, @"~/default.aspx?a=$1");
                Context.RewritePath(realPath);
            }

        }

2、写一个Module,然后在webconfig中配置启动。

public class URLRewriterModule : IHttpModule
    {
        public void Init(HttpApplication context)
        {
            context.BeginRequest += (s, e) =>
                {
                    HttpApplication app = s as HttpApplication;
                    var path = app.Context.Request.AppRelativeCurrentExecutionFilePath;

                    Regex regex = new Regex(@"~/default-(\d+)");
                    if (regex.IsMatch(path))
                    {
                        var realPath = regex.Replace(path, @"~/default.aspx?a=$1");
                        app.Context.RewritePath(realPath);
                    }

                };

        }

        public void Dispose()
        { }

webconfig中system.webServer的节点下配置

  <system.webServer>
    <!--设置不去校验过时配置-->
    <!--<validation validateIntegratedModeConfiguration="false"/>-->
    
    <modules>
      <!---namez自定义随便写,type写完整路径名称和网站程序集名称-->
      <add name="myurlrewriter" type="UrlRewriterForm.URLRewriterModule,UrlRewriterForm"/>
    </modules>
    <defaultDocument>
      <files>
        <!--<add value="index.aspx"/>-->
      </files>
    </defaultDocument>
  </system.webServer>

如果是老版本iis,是在system.web的节点配置

 <httpModules>
      <!--老版本iis使用-->
      <add name="myurlrewriter" type="UrlRewriterForm.URLRewriterModule,UrlRewriterForm"/>
    </httpModules>

并且在system.webServer下配置

  <!--设置不去校验过时配置-->
    <validation validateIntegratedModeConfiguration="false"/>

3、使用第三方的URLRewriter,可以用NuGet获取包,原理应该和2是一样的,只不过将url重写规则放在配置中,然后module中读取配置信息。安装完包之后webconfig中自动增加了一些信息:

  <configSections>
    <section name="rewriter" requirePermission="false" type="Intelligencia.UrlRewriter.Configuration.RewriterConfigurationSectionHandler, Intelligencia.UrlRewriter" />
  </configSections>
  <httpModules>         
      <add type="Intelligencia.UrlRewriter.RewriterHttpModule, Intelligencia.UrlRewriter" name="UrlRewriter" />      
    </httpModules>

system.webServer下面出现rewriter

<rewriter>
    <redirect url="~/Default.aspx" to="~/Default" />
    <redirect url="~/Login.aspx" to="~/Login" />
  </rewriter>

HTTP 错误 500.22 - Internal Server Error
检测到在集成的托管管道模式下不适用的 ASP.NET 设置。

将配置迁移到 system.webServer/modules 节。
如果您确信可以忽略此错误,则可以通过将 system.webServer/validation@validateIntegratedModeConfiguration 设置为 false 来禁用它。

  <system.webServer>
    <!--设置不去校验过时配置-->
    <validation validateIntegratedModeConfiguration="false"/>
    <modules>    
      <add type="Intelligencia.UrlRewriter.RewriterHttpModule, Intelligencia.UrlRewriter" name="UrlRewriter" />
    </modules>
    <defaultDocument>
      <files>
        <!--<add value="index.aspx"/>-->
      </files>
    </defaultDocument>
  </system.webServer>

4、用第三方插件直接在iis服务器上修改。

isapi_rewrite

http://www.helicontech.com/isapi_rewrite/download.html

Free URL Rewriter

http://www.microsoft.com/web/spotlight/urlrewriter/

页面伪静态

标签:

原文地址:http://www.cnblogs.com/tgdjw/p/4616002.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!