ASP.NET MVC URL路由 [英] asp.net mvc url routing

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

问题描述

如何映射诸如domain.com/username之类的内容?问题是我认为MVC路由会寻找控制器以确定其应如何处理映射请求.

How do I map something like domain.com/username? The problem is I think that the MVC routing looks for the controller to determine how it should handle the mapping request.

我对ASP.NET MVC还是很陌生.

I am pretty new to ASP.NET MVC.

但是,根据到目前为止的教程,路由机制似乎相当僵化.

However, based on the tutorials so far, the routing mechanism seems rather rigid.

推荐答案

它实际上非常灵活,我想您会发现它具有更多的经验,功能非常强大.您可以按照以下方式做自己想做的事:

It's actually quite flexible, I think you'll find it's quite powerful with some more experience with it. Here's how you can do what you want:

routes.MapRoute("MyRoute", 
                 "{username}", 
                 new { controller = "Home", action = "Index", username = "" });

这将选择默认控制器(主页")和默认操作方法(索引"),并向其传递一个用户名参数(默认情况下设置为").

This chooses a default controller ("Home") and a default action method ("Index"), and passes it a username parameter (which is set to "" by default).

请谨慎使用此路由,因为它实际上会匹配您可以想象的任何URL.它应该是您添加到映射中的最后一条路线,因此您的其他路线最先出现在URL上.

Careful with this route though, because it will match virtually any URL you can imagine. It should be the last route that you add to your mappings, so your other routes have a chance at the URL first.

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

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