gpt4 book ai didi

c# - 路由 MVC ASP.Net 将无法正常工作

转载 作者:行者123 更新时间:2023-11-30 23:11:12 25 4
gpt4 key购买 nike

即使在提出许多其他类似问题之后,我也无法完成这项工作。这是我的 Controller (使用 Entity Framework 创建)

namespace mypage.Controllers
{
[RoutePrefix("Booking")]
public class BookingsController : BaseController
{
private mypageContext db = new mypageContext();

// GET: Bookings
public ActionResult Index()
{
var model = db.Bookings.ToList();
//model.Find()
return View(model);
}
// etc.

我的 global.asax

public class MvcApplication : System.Web.HttpApplication
{
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
Database.SetInitializer<mypageContext>(new DropCreateDatabaseIfModelChanges<mypageContext>());
}
}

和routeconfig.cs

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

我在打开 http://localhost:xxxxx/Booking 时总是得到 404然而这有效http://localhost:xxxxx/Bookings

还有其他地方要检查吗?

PS: BaseController 是空的

public abstract class BaseController : Controller
{

}

编辑

但是,如果我添加一个常规 前缀而不是 RoutePrefix,它也根本不起作用

[Route("Booking/New")]
public ActionResult Edit(int? id)

最佳答案

如果为 Controller 指定RoutePrefix 属性,则需要为操作指定Route 属性。

[RoutePrefix("TestPrefix")]
public class TestController : Controller
{
[Route("TestAction")]
public ActionResult TestAction()
{
//.........
return View();
}
}

关于c# - 路由 MVC ASP.Net 将无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44864736/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com