2
即使经过几十个其他类似的问题,我仍无法获得此项工作。这是我的控制器(与实体框架创建)路由MVC ASP.Net不会工作
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 }
);
}
}
我总是得到一个404打开http://localhost:xxxxx/Booking 这个然而,当作品http://localhost:xxxxx/Bookings
其他还有其他地方要检查吗?
PS:在BaseController
就是一句
public abstract class BaseController : Controller
{
}
编辑
如果我不过增加的,而不是一个RoutePrefix定期前缀,它不会在所有无论是
[Route("Booking/New")]
public ActionResult Edit(int? id)
工作
好的,后续问题,因为这实际上工作。我现在真的需要为每个动作添加“[Route(”ThisAction“)]”吗? – rst
您可以用'[RoutePrefix(“Booking”),Route(“{action}”)]'标记控制器,并且在每个操作中都不需要它。这里有很多例子[ASP.NET MVC 5中的属性路由](https://blogs.msdn.microsoft.com/webdev/2013/10/17/attribute-routing-in-asp-net-mvc-5/) – Jasen
如果我在这里添加了[[Route(“test”)]',我得到一个'HTTP Error 400.0 - Bad Request' ....为什么? – rst