Asp.net MVC自定义路由 [英] Asp.net MVC custom routing

查看:182
本文介绍了Asp.net MVC自定义路由的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要创建此URL的自定义路由:

  / PAR1 / PAR2 / PAR3 / par99 / 11

URL必须重定向到

  

11是ID

我只需要这个参数,其他参数( PAR1 / PAR2 /...)仅适用于搜索引擎优化的目的,可以是任何文字。

PAR1,PAR2等。是动态创建的,因此,URL可以是:

 / PAR1 / PAR2 / 11 

 / PAR1 / PAR2 / PAR3 / 111 

 / PAR1 / 3 


解决方案

您可以只将其翻过来,以

  routes.MapRoute(
    路线,
    {ID} / {*} seostuff
    新{控制器=家,行动=索引,SEO = UrlParameter.Optional});

这将允许您将URL映射如的 http://www.somesite.com/11/whatever/goes-here/will-be-whatever-you/want

I need to create a custom routing for this url:

/par1/par2/par3/par99/11

The url must redirect to

home/index/11

11 is the Id

I need only this parameter, the other parameter (par1/par2/...) are only for SEO purpose and could be any word.

par1, par2, etc.. are created dynamically , so the Url could be:

/par1/par2/11

or

/par1/par2/par3/111

or

/par1/3

解决方案

you could just turn it around and go with

routes.MapRoute(
    "Route",
    "{id}/{*seostuff}",
    new {controller = "Home", action="Index", seo = UrlParameter.Optional});

that will allow you to map urls such as http://www.somesite.com/11/whatever/goes-here/will-be-whatever-you/want

这篇关于Asp.net MVC自定义路由的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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