【问题标题】:How to change default url in ASP.NET MVC?如何更改 ASP.NET MVC 中的默认 URL?
【发布时间】:2021-09-18 19:36:41
【问题描述】:

我有一个显示数据库值的表,我有 3 个这样的 @Html.ActionLink

                        @Html.RouteLink("Edit", "StudentGet", new { id = item.StudentId }) |
            @Html.ActionLink("Details", "Details", new { id=item.StudentId }) |
            @Html.ActionLink("Delete", "Delete",  new { id=item.StudentId })

这些是在我创建实体控制器时自动构建的。当我点击这些链接时,我会进入新的 URL:Students(my controller name)/Edit/1.(例如)。

我怎样才能将我的网址更改为类似的其他内容:stu-info/edit-info/id=1

另外,这里是我的RouterConfig.cs:

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

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

并在 StudentsController

中操作 Edit
[Route("~/stu-info/get-info/{id?}", Name = "StudentGet")]
        public ActionResult Edit(byte? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            Student student = db.Students.Find(id);
            if (student == null)
            {
                return HttpNotFound();
            }
            return View(student);
        }
[Route("~/stu-info/edit-info")]
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit([Bind(Include = "StudentId,LirstName,LastName,Age,SchoolName")] Student student)
        {
            if (ModelState.IsValid)
            {
                db.Entry(student).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(student);
        }

我尝试更改@Html.ActionLink 中的第二个参数,它可以更改 URL(我不知道如何更改学生的 URL)但它不起作用,因为它可能无法识别编辑行动。

【问题讨论】:

    标签: c# asp.net-mvc url routes


    【解决方案1】:

    最简单的方法是使用属性路由

    [Route("~/stu-info/get-info/{id?}" Name="StudentGet")]
    public ActionResult Edit(byte? id)
    ......
      
    [Route("~/stu-info/edit-info" )]
    public ActionResult Edit(Student student)
    .....
    

    也许您需要更改您的操作链接以获得也

     @Html.RouteLink("Edit", "StudentGet", new { id=item.StudentId }) 
    

    并将 MapMvcAttributeRoutes 添加到您的路线配置中

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

    【讨论】:

    • 我还有什么需要做的吗?因为我收到错误“在路由集合中找不到名为'StudentGet'的路由。”
    • @ĐạtNguyễn 你用的是什么版本的网络?我更新了我的答案。
    • "Microsoft.AspNet.Mvc" 版本="5.2.7",
    • 修复 registerRoutes 并重试
    • 问题是我忘了加:routes.MapMvcAttributeRoutes();但接下来的错误是“找不到资源”。有什么我错过的吗?
    【解决方案2】:

    最简单的方法是使用属性路由

    [路线(“登录”)]

    public ActionResult Login(string ReturnUrl = "")

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-08-17
      • 1970-01-01
      • 2011-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-10-28
      • 1970-01-01
      • 2016-10-05
      相关资源
      最近更新 更多