当参数涉及如何指定ASP.NET 4.0 URL映射? [英] How to specify url mappings in ASP.NET 4.0 when parameters are involved?

查看:319
本文介绍了当参数涉及如何指定ASP.NET 4.0 URL映射?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题

我有一个网站,里面的一些文章。文章使用的是单页上的数据库结果写入访问。当某个物品被访问,我需要的文章ID 的,我只需要得到页面: HTTP://www.mysite/Articles.aspx? A = {文章ID}

这是好的,但不是真正的友好,而不是可扩展性。我想要做的就是每个URL重定向形式: HTTP://www.mysite/articles/ {文章ID} 页面 HTTP:?//www.mysite/Articles.aspx A = {文章ID}

我知道,使用的Web.Config 我可以使用 urlMappings 设置,但不知道如何插入正则表达式那里。

的几点说明

请考虑,我想有一个简单的翻译工具。我不希望我的编辑现有的网页。该溶液应尽可能透明到我的应用程序组件。

什么是去的解决方案的最佳实践?
三江源


解决方案

我认为你应该使用:


  1. ASP.NET路由

例如:

 保护无效的Application_Start(对象发件人,EventArgs的发送)
{
    的RegisterRoutes(RouteTable.Routes);
}公共静态无效的RegisterRoutes(RouteCollection路线)
{
    routes.MapPageRoute(公司章程,
        的文章/ {文章ID} /,
        〜/ Articles.aspx);
}

&安培;在Articles.aspx.cs就可以得到文章ID使用:

 串文章ID = Page.RouteData.Values​​ [文章ID]作为字符串;


  1. 使用 URL重写模块


结果
结果
结果


  1. urlrewriter.net

The problem

I have a web site with some articles inside. Articles are accessed using a database writing results on a single page. When a certain article is to be accessed, I need the article-id and I only need to get to the page: http://www.mysite/Articles.aspx?a={article-id}.

This is nice but not really friendly and not scalable. What I want to do is to redirect every url in the form: http://www.mysite/articles/{article-id} to the page http://www.mysite/Articles.aspx?a={article-id}.

I know that using Web.Config I can use the urlMappings settings, but do not know how to insert a regex there.

Some notes

Please consider that I would like to have a simple translation tool. I do not want to edit my existing pages. This solution should be as transparent as possible to my application components.

What is the best practice to get to the solution? Thankyou

解决方案

I think you should use:

  1. ASP.NET Routing

Eg.:

protected void Application_Start(object sender, EventArgs e)
{
    RegisterRoutes(RouteTable.Routes);
}

public static void RegisterRoutes(RouteCollection routes)
{
    routes.MapPageRoute("Articles",
        "articles/{article-id}/",
        "~/Articles.aspx");
}

& on "Articles.aspx.cs" you can get the "article-id" using:

string article-id = Page.RouteData.Values["article-id"] as string;

  1. Using the URL Rewrite Module




  1. urlrewriter.net

这篇关于当参数涉及如何指定ASP.NET 4.0 URL映射?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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