【问题标题】:Routing in .net MVC 5.net MVC 5 中的路由
【发布时间】:2014-12-09 18:57:59
【问题描述】:

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

【问题讨论】:

  • 您能否使用您的主机文件将test.localhostgain.localhost 指向与localhost 相同的IP 地址?
  • 不。我不知道该怎么做。
  • 我已经解决了,谢谢。

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


【解决方案1】:

//在RouteConfig.cs中添加2个这样的方法

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);
    }

该功能为讲师姓名提供了新的路线。所以你应该尝试这样的事情。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-07-15
    • 1970-01-01
    • 2016-04-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多