C# .NETMVC5中的路由

C# .NETMVC5中的路由,c#,asp.net-mvc,asp.net-mvc-5,asp.net-mvc-routing,C#,Asp.net Mvc,Asp.net Mvc 5,Asp.net Mvc Routing,我试图通过我的mvc代码重新路由我的url请求。在我的本地系统上。我使用localhost/site,我希望能够使用test.localhost/site、gain.localhost/site等,它们都应该指向localhost/site。如果是的,C#或vb.net代码示例将非常受欢迎,这是可能的。过去一周我一直在研究这个问题,我真的需要社区的帮助。//在RouteConfig.cs中添加两个类似的方法 public static void PeopleRoute(RouteCollecti

我试图通过我的mvc代码重新路由我的url请求。在我的本地系统上。我使用localhost/site,我希望能够使用test.localhost/site、gain.localhost/site等,它们都应该指向localhost/site。如果是的,C#或vb.net代码示例将非常受欢迎,这是可能的。过去一周我一直在研究这个问题,我真的需要社区的帮助。

//在RouteConfig.cs中添加两个类似的方法

public static void PeopleRoute(RouteCollection routes)
    {
        DBContext db = new DBContext();

        List<Instructor> instructors = db.Instructors.ToList();

        foreach (Instructor ins in instructors)
        {
            routes.MapRoute(
                name: "Instructor_" + ins.UserName,
                url: ins.UserName,
                defaults: new { controller = "People", action = "Details", id = ins.UserName }
            );
        }
    }

    public static void RefreshRouteTable()
    {
        // This method should be called when
        // 1) A new instructor is added
        // 2) An existing instructor is deleted
        // 3) Username of an existing instructor is changed.
        RouteTable.Routes.Clear();
        RouteConfig.RegisterRoutes(RouteTable.Routes);
    }
公共静态无效人员路线(路线收集路线)
{
DBContext db=new DBContext();
List instructors=db.instructors.ToList();
foreach(讲师中的讲师ins)
{
routes.MapRoute(
名称:“讲师”+ins.UserName,
url:ins.UserName,
默认值:新建{controller=“People”,action=“Details”,id=ins.UserName}
);
}
}
公共静态无效RefreshRouteTable()
{
//应在以下情况下调用此方法:
//1)增加一名新教员
//2)删除现有讲师
//3)更改现有讲师的用户名。
RouteTable.Routes.Clear();
RouteConfig.RegisterRoutes(RouteTable.Routes);
}

该函数为讲师姓名提供了新的路径。因此,您应该尝试这样做。

您可以使用主机文件将
test.localhost
gain.localhost
指向与
localhost
相同的IP地址吗?不可以。我不知道怎么做,我已经修好了,谢谢。