rst
rst

Reputation: 2734

Route MVC ASP.Net just won't work

I can't get this work, even after dozens of other similar questions. Here is my controller (created with 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.

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

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

I always get a 404 when opening http://localhost:xxxxx/Booking this however works http://localhost:xxxxx/Bookings

Is there any other spot to check?

PS: the BaseController is just empty

public abstract class BaseController : Controller
{

}

Edit

If I however add instead of RoutePrefix a regular Prefix, it doesn't work at all either

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

Upvotes: 2

Views: 436

Answers (1)

Isuru
Isuru

Reputation: 966

You need to specify Route attribute for the action if you specify RoutePrefix attribute for the controller.

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

Upvotes: 2

Related Questions