使URL在ASP.Net用户友好 [英] Make URL in ASP.Net user-friendly

查看:157
本文介绍了使URL在ASP.Net用户友好的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用Web窗体来开发我的第一个网站在ASP.Net。

我有一些控件和一个TextBox控件的形式。虽然现在我使用 GET 请求。当用户提交表单其浏览器期望得到长的URL,像

<$p$p><$c$c>http://mysite.com/search.aspx?__VIEWSTATE=%2FwEPDwUJNTE2NjY5jMY4D2QWAgICD2QWAgIDDW8wAh4EVGV4dAUBMWRkZKthQ0zeIP5by49qIHwSuW6nOj8iLTdoCUzpH369xyg8&__EVENTVALIDATION=%2FwEWAwLnrcHhBQLs0bLrBgKM54rGBjGtX5fJOylLy4qRbt6DqPxO%2FnfcMOkHJBRFqZTZdsBD&TextBox1=sfs&Button1=Button

如果他的投入是 TextBox1中 A字 SFS
所以,我需要回到他的反应。我想表现出像

一个用户友好的URL该响应

http://mysite.com/search.aspx?TextBox1=sfs


http://mysite.com/sfs


http://mysite.com/search/sfs

我怎样才能做到这一点?如果我使用Response.Redirect的,它首先会返回302,才把短网址工作。 Server.Transfer的不改变网址,用户将看到丑陋长的URL浏览器。

在我看来,它有可能通过解决 RouteCollection.MapPageRoute 这就出现了4.0框架,但目前还不清楚给我,我如何使用它。

任何帮助是AP preciated。

更新。它不使用 POST 而不是 GET 的问题。但这种方式的网址始终看起来象 http://mysite.com/search.aspx

UPDATE2。该形式必须是服务器控件,它有除提交和文本框控件的另一个。如果这个参数没有出现在网址示出了在浏览器将是良好的(虽然仍然,没有必要的。


解决方案

自GET请求,您还可以使用JavaScript,设置

  location.href ='http://mysite.com/search/'+查询;

然后在ASP.NET端,您可以使用 URL重写功能,该网址到一个特定的ASPX页面重定向作为查询字符串参数。

让我知道如果你想更详细的样本。

示例:

下面是一个示例,请注意我没有测试它,但这应该让你开始。

 &LT; HTML和GT;
&LT; HEAD&GT;
  &LT;脚本类型=文/ JavaScript的&GT;
    功能searchRedirect()
    {
      VAR的查询= $ GET('查询');
      location.href =/搜索/+ query.value;
    }
  &LT; / SCRIPT&GT;
&LT; /头&GT;
&LT;身体GT;
    &LT; D​​IV CLASS =搜索&GT;
        &LT;输入类型=文本ID =查询/&GT;&LT; BR /&GT;
        &LT;输入类型=按钮ID =搜索VALUE =搜索的onclick =searchRedirect(); /&GT;
    &LT; / DIV&GT;
&LT; /身体GT;
&LT; / HTML&GT;

然后在你必须有这样的RouteModule重定向方:

 公共类UrlRewriter:IHttpModule的
{
    公共无效的Dispose()
    {
    }    公共无效初始化(HttpApplication的情况下)
    {
        context.AuthorizeRequest + =新的EventHandler(OnBeginRequest); //这将确保登录页面有vitual URL不是映射的URL
    }
    私人无效OnBeginRequest(对象发件人,EventArgs的发送)
    {
        VAR应用=发件人作为HttpApplication的;
        如果(应用程序!= NULL)
        {
            VAR requestPath = application.Request.Ap prelativeCurrentExecutionFilePath;
            如果(requestPath.ToLower()。StartsWith(/搜索/))
            {
                变种查询= requestPath.Substring(8);
                application.Context.RewritePath(Search.aspx空,查询=+查询,假);
            }
            // ..其它路线
        }
    }
}

和假设code是你的APP_ code文件夹,你可以在你的web.config使用

 &LT;&的System.Web GT;
  &LT;! - ... - &GT;
  &LT;&的HttpModules GT;
      &LT;添加名称=UrlRewriterTYPE =UrlRewriter,__ code/&GT;
  &LT; /&的HttpModules GT;
&LT; /system.web>&LT;! - 如果IIS7 - &GT;
&LT; system.webServer&GT;
  &LT;模块&gt;
    &LT;添加名称=UrlRewriterTYPE =UrlRewriter,__ code/&GT;
  &LT; /模块&gt;
&LT; /system.webServer>

I'm trying to develop my first site in ASP.Net using Web Forms.

I have a form with some controls and a TextBox control. While now I use GETrequest. When user submits a form his browser expects to get long URL, something like

http://mysite.com/search.aspx?__VIEWSTATE=%2FwEPDwUJNTE2NjY5jMY4D2QWAgICD2QWAgIDDW8wAh4EVGV4dAUBMWRkZKthQ0zeIP5by49qIHwSuW6nOj8iLTdoCUzpH369xyg8&__EVENTVALIDATION=%2FwEWAwLnrcHhBQLs0bLrBgKM54rGBjGtX5fJOylLy4qRbt6DqPxO%2FnfcMOkHJBRFqZTZdsBD&TextBox1=sfs&Button1=Button

if his input is a word sfs in TextBox1. So I need to return him response. I would like to show this response on a user-friendly URL like

http://mysite.com/search.aspx?TextBox1=sfs

or http://mysite.com/sfs

or http://mysite.com/search/sfs

How can I do that? If I use Response.Redirect, it first returns 302, and only then work on short URL. Server.Transfer doesn't change URL and user sees ugly long URL in browser.

It seems to me that it is possible to solve via RouteCollection.MapPageRoute which appeared in 4.0 Framework but it's unclear to me how I can use it.

Any help is appreciated.

UPDATE. It is not a problem to use POST instead of GET. But in this way URL will always look like http://mysite.com/search.aspx

UPDATE2. The form MUST be server control and it has another controls except submit and textbox. It would be good (though, still, not necessary if this parameters don't appear in URL showing in the browser.

解决方案

Since its a GET request you can also use javascript, setting the

location.href = 'http://mysite.com/search/' + query; 

Then on the ASP.NET side you can use the URL Rewriting feature to redirect that url to a specific ASPX page as a query string parameter.

Let me know if you would like a more detailed sample.

Sample:

Here is a sample, please note I haven't tested it, but this should get you started.

<html>
<head>
  <script type="text/javascript">
    function searchRedirect()
    {
      var query = $get('query');
      location.href = "/search/" + query.value;
    }
  </script>
</head>
<body>
    <div class="search">
        <input type="text" id="query" /><br />
        <input type="button" id="search" value="Search" onclick="searchRedirect();" />
    </div>
</body>
</html>

Then on the redirect side you have have a RouteModule like this:

public class UrlRewriter : IHttpModule
{
    public void Dispose()
    {
    }

    public void Init(HttpApplication context)
    {
        context.AuthorizeRequest += new EventHandler(OnBeginRequest); //this ensures the login page has the vitual url not the mapped url
    }


    private void OnBeginRequest(object sender, EventArgs e)
    {
        var application = sender as HttpApplication;
        if (application != null)
        {
            var requestPath = application.Request.AppRelativeCurrentExecutionFilePath;
            if (requestPath.ToLower().StartsWith("/search/"))
            {
                var query = requestPath.Substring(8);
                application.Context.RewritePath("Search.aspx", null, "query=" + query, false);
            }
            // .. Other Routes
        }
    }
}

And assuming the code is in your App_Code folder you could use this in your web.config

<system.web>
  <!-- ... -->
  <httpModules>
      <add name="UrlRewriter" type="UrlRewriter, __code"/>
  </httpModules>
</system.web>

<!-- If IIS7 -->
<system.webServer>
  <modules>
    <add name="UrlRewriter" type="UrlRewriter, __code" />
  </modules>
</system.webServer>

这篇关于使URL在ASP.Net用户友好的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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