2013-12-19 156 views
1
页面

我有一个非常简单的MVC应用程序:MVC路由索引

当我键入:

http://locahost:8080 

在routeconfig以下路线带我到主控制器:

routes.MapRoute(
       name: "Default", 
       url: "{controller}/{action}/{id}", 
       defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional } 
      ); 

当我输入以下内容时,我收到404错误。

http://locahost:8080/JohnDoe 

我想将这个请求映射到Home Controller的带有名称函数的Get Action(见下面)。我该如何去做呢?

public Person Get(string name) 
    { 
     PersonRespository db = new PersonRespository(); 
     return db.GetPerson(name); 
    } 

非常感谢。

+0

您的操作名称为Index,但您的方法称为Get。调用你的方法索引。 – Tim

回答

2

以下应该工作。

routes.MapRoute(
    name: "Custom", 
    url: "{name}", 
    defaults: new 
    { 
     controller = "Home", 
     action = "Get" 
    }); 
+0

工作。非常感谢@emre –