如何使用C#将短网址重定向到长网址 - ASP.NET [英] How to redirect short url to long url using C# - ASP.NET
问题描述
我需要在asp中对303状态代码进行页面重定向。当我在浏览器中提供tempURL(手动生成)时,链接必须自动重定向到原始URL。我将为每个tempURL和相应的原始URL ..我需要动态页面重定向..请不要使用web.config文件为静态重定向提供解决方案..
如果我的webconfig为
< 重写者 >
< 重写 url = http://www.mycompany.in/ 至 = originalUrlMat chedFromDBForUSerRequest / >
< / rewriter >
如何动态更改 originalUrlMatchedFromDBForUSerRequest ?
在web.config中添加:
< customErrors mode = RemoteOnly defaultRedirect = 〜/ errorsfolder / ErrorPage.aspx redirectMode = ResponseRewrite >
< 错误 statusCode = 303 重定向 = 〜/ errorsfolder / yourerrorpage.aspx / >
< ; / customErrors >
Response.RedirectLocation = url;
Response.StatusCode = 303 ;
Ur参考 http://stackoverflow.com/questions/9497467/how-to-create-303-response-in-asp-net [ ^ ]
所以你应该去 ASP.NET中的自定义错误 [ ^ ]
试试这个:
< customerrors defaultredirect = 〜/ SomeErrorPage.htm mode = On >
< 错误 < span class =code-attribute> 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://stackoverflow.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
这篇关于如何使用C#将短网址重定向到长网址 - ASP.NET的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!