简体   繁体   English

在asp.net中重写URL

[英]rewriting url in asp.net

hey guys, i am using asp.net 2.0 and dotnetnuke 5.6, and using a crossarticle module, now i want that user can create multiple alias for the article. 大家好,我正在使用asp.net 2.0和dotnetnuke 5.6,并使用跨文章模块,现在我希望该用户可以为文章创建多个别名。

say like below. 像下面这样说。 article/stackoverflow stackexchange/article etc.. article / stackoverflow stackexchange / article等。

say first article is linked with article id 1, and second is linked with articleid 2 then they should jump to their respective pages.. like article/1 or article/2 假设第一个文章与文章ID 1关联,第二个文章与文章ID 2关联,那么他们应该跳到各自的页面。.例如article / 1或article / 2

i can do this from web.config file... but the problem is there could be lacs of alias'es, so maintaining it in web.config file is totally flop idea. 我可以从web.config文件中执行此操作...但是问题是可能存在别名错误,因此将其保存在web.config文件中完全是个失败的主意。 so i want to do it with global.asax file.. 所以我想用global.asax文件来做。

i tried below code: 我试过下面的代码:

in string variable i saved: Request.RawUrl.ToString() and then comparing it with the url user entered and then redirecting him to appropriate page... 在我保存的字符串变量中:Request.RawUrl.ToString(),然后将其与输入的url用户进行比较,然后将其重定向到适当的页面...

but i am having below issues: i dont want user to redirect i want to rewrite url, ie, if user enters article/stackoverflow... he should be redirected to article/1 page but url should retain. 但是我遇到以下问题:我不希望用户重定向我想重写url,即,如果用户输入article / stackoverflow ...他应该被重定向到article / 1页面,但url应该保留。

Please anyone suggest something for this issue.. 请任何人对此问题提出建议。

To rewrite the URL (as opposed to redirecting) you can do something like this: 要重写URL(相对于重定向),您可以执行以下操作:

private static void RewriteUrl(HttpApplication application, TabInfo tabInfo, string additionalQueryString)
{
    application.Context.Items["UrlRewrite:OriginalUrl"] = application.Request.Url.AbsoluteUri;

    var portalAliasInfo = PortalAliasController.GetPortalAliasInfo(application.Context.Request.Url.DnsSafeHost);        
    application.Context.Items["PortalSettings"] = new PortalSettings(tabInfo.TabID, portalAliasInfo);

    var queryString = String.Format("tabid={0}" + additionalQueryString, tabInfo.TabID);
    application.Context.RewritePath("~/default.aspx", string.Empty, queryString, false);
}

I'd recommend doing it in an httpmodule though instead of modifying the core code. 我建议您在httpmodule中执行此操作,而不要修改核心代码。

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

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