【问题标题】:MVC Routing the Index pageMVC 路由索引页面
【发布时间】:2013-12-19 15:27:13
【问题描述】:

我有一个非常简单的 MVC 应用程序:

当我输入时:

http://locahost:8080

routeconfig 中的以下路由将我带到 Home 控制器:

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 with name 函数(见下文)。我该怎么做呢?

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

非常感谢各位。

【问题讨论】:

  • 您的操作名称是 Index,但您的方法称为 Get。调用你的方法 Index。

标签: c# .net asp.net-mvc routing


【解决方案1】:

以下应该可以工作。

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

【讨论】:

    猜你喜欢
    • 2015-02-25
    • 2014-02-07
    • 1970-01-01
    • 1970-01-01
    • 2018-12-28
    • 2017-01-31
    • 2015-06-03
    • 2015-08-26
    • 2022-11-20
    相关资源
    最近更新 更多