c# - HTTP 错误 404.0 - 未找到 - MVC 属性路由
问题描述
我正在尝试学习 MVC 5 属性路由。
我启用了属性路由
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
namespace Vidly
{
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapMvcAttributeRoutes();
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
}
}
我在MoviesController.cs
文件中定义了属性路由
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Vidly.Models;
namespace Vidly.Controllers
{
public class MoviesController : Controller
{
// GET: Movies
public ActionResult Random()
{
var movie = new Movie() { Name = "Shrek!" };
//return View(movie);
// return Content("Hello World");
// return HttpNotFound();
//return new EmptyResult();
return RedirectToAction("Index", "Home", new { page = 1, sortBy = "name" });
}
public ActionResult Edit(int id)
{
return Content("id=" + id);
}
[Route("Movies/released/{year}/{month:regex(\\d{2)}")]
public ActionResult ByReleaseYear(int year,int month)
{
return Content(year+"/"+ month);
}
}
}
我仍然不断得到
HTTP 错误 404.0 - 找不到 类似的 URL
http://localhost:51946/Movies/released/1243/12
解决方案
您错过了正则表达式中的一个括号。而不是"released/{year}/{month:regex(\\d{2)}"
应该是"Movies/released/{year}/{month:regex(\\d{2})}"
.
因此,以下属性将起作用:
[Route("Movies/released/{year}/{month:regex(\\d{2})}")]
推荐阅读
- javascript - 从 Chrome 扩展脚本到(同源)动态添加的 iframe 的点击无效
- javascript - 在javascript中修改对象的键
- macos - VScode 无法找到颤振 sdk 目录
- c# - 如何使用 IdentityServer4.AspNetIdentity 解决网页问题
- c# - Fluent API 与 FluentValidation
- javascript - 与 babel polyfill 混淆
- kotlin - Kotlin 中主构造函数、次构造函数和类成员如何交互?
- android - 我想通过使用 admob 在我的另一个应用中插入插页式广告来宣传我的一个应用
- javascript - 给定 OpenSSL.SSL.Error 'SSL 例程','tls_process_server_certificate','证书验证失败,通过 requests-html 获取 JavaScript Web
- terraform - 如何使用 terraform 启用控制台登录创建多个 iam 用户