尝试使用路由设置根主页 [英] Trying to setup root homepage using routing

查看:58
本文介绍了尝试使用路由设置根主页的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试设置域的根路径 http://www.example.com/使用路由"模块连接到 PersonSearch 控制器,但似乎没有任何效果(404错误).

I am trying to set the root path of my domain http://www.example.com/ to the PersonSearch controller using the Routing module, but it doesn't seem to be having any effect (404 error).

URL http://www.example.com/person/search 正确地带我到所需的页面.

The URL http://www.example.com/person/search correctly takes me to the desired page.

RouteConfig.cs

public class RouteConfig
{
  public static void RegisterRoutes(RouteCollection routes)
  {
    routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

    routes.MapMvcAttributeRoutes();

    routes.MapRoute(
      "Root",
      "",
      defaults: new { controller = "Person", action = "Search" }
    );
  }
}

PersonController.cs

public class PesonController : Controller
{
    [HttpGet]
    [Route("person/search")]
    public ActionResult Search()
    {
        PersonSearchViewModel psvm = new PersonSearchViewModel();
        return View(psvm);
    }
}

推荐答案

我发现我可以用一个简单的属性路由"代码来实现我想要的.

I found out that I could achieve what I wanted with a simple Attribute Routing piece of code.

public class PesonController : Controller
{
    [HttpGet]
    [Route("~/")]
    [Route("person/search")]
    public ActionResult Search()
    {
        PersonSearchViewModel psvm = new PersonSearchViewModel();
        return View(psvm);
    }
}

然后我可以在 RouteConfig

这篇关于尝试使用路由设置根主页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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