且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何使用C#将短网址重定向到长网址 - ASP.NET

更新时间:2023-11-27 19:46:04

在web.config中添加:



 <   customErrors     mode   =  RemoteOnly    defaultRedirect   = 〜/ errorsfolder / ErrorPage.aspx    redirectMode   =  ResponseRewrite >  
< 错误 statusCode = 303 重定向 = 〜/ errorsfolder / yourerrorpage.aspx / >
&lt ; / customErrors >



 Response.RedirectLocation = url; 
Response.StatusCode = 303 ;







Ur参考 http://***.com/questions/9497467/how-to-create-303-response-in-asp-net [ ^ ]


所以你应该去 ASP.NET中的自定义错误 [ ^ ]

试试这个:

 <   customerrors     defaultredirect   = 〜/ SomeErrorPage.htm    mode   =  On >  
< 错误 statuscode = 303 重定向 = 〜/ YourPage.htm / >
< / customersrors >



同时检查显示自定义错误页面(C#) [ ^ ]。





在您的母版页中试试这个:

 受保护  void  Page_Load( object  sender,EventArgs e)
{
if (Response.StatusCode == 303
{
switch (Request.Url.AbsoluteUri)
{
case 您的第一个LongURL
Response.Redirect( 您的第一个SortURL);
break ;
case 你的第二个LongURL
Response.Redirect( 你的第二个SortURL);
break ;
case 你的第3个LongURL
Response.Redirect( Your 3rd SortURL);
break ;
默认
break ;
}
}
}



[/编辑]



--Amit


I need to do Page redirection of 303 status code in asp.. When I provide the tempURL(manually generated) in browser the link has to be automatically redirected to the original URL.. I will be having DB for each tempURLs and corresponding original URL.. I need the dynamic page redirection.. Please don''t provide solution for static redirects using web.config file..

if I have my webconfig as

<rewriter>
     <rewrite url="http://www.mycompany.in/" to=originalUrlMatchedFromDBForUSerRequest />
</rewriter>



How to change "originalUrlMatchedFromDBForUSerRequest" dynamically?

Add this in web.config:

<customErrors mode="RemoteOnly" defaultRedirect="~/errorsfolder/ErrorPage.aspx" redirectMode="ResponseRewrite">
           <error statusCode="303" redirect="~/errorsfolder/yourerrorpage.aspx"/>
       </customErrors>


Try this
Response.RedirectLocation = url;
 Response.StatusCode = 303;




For Ur Reference http://***.com/questions/9497467/how-to-create-303-response-in-asp-net[^]


So you should go for Custom Errors in ASP.NET[^]
Try this:
<customerrors defaultredirect="~/SomeErrorPage.htm" mode="On">
   <error statuscode="303" redirect="~/YourPage.htm" />
</customerrors>


Also check Displaying a Custom Error Page (C#)[^].

[Edit]
Try this in your master page:

protected void Page_Load(object sender, EventArgs e)
{
    if (Response.StatusCode == 303)
    {
        switch (Request.Url.AbsoluteUri)
        {
            case "Your 1st LongURL":
                Response.Redirect("Your 1st SortURL");
                break;
            case "Your 2nd LongURL":
                Response.Redirect("Your 2nd SortURL");
                break;
            case "Your 3rd LongURL":
                Response.Redirect("Your 3rd SortURL");
                break;
            default:
                break;
        }
    }
}


[/Edit]

--Amit