Knerd
Knerd

Reputation: 2002

Routing not working in razor

I have problems with the routing for my razor mvc 4 site.

The Newssite is in a custom area, here ist the NewsAreaRegistration

using System.Web.Mvc;

namespace Web.Areas.News {
    public class NewsAreaRegistration : AreaRegistration {
        public override string AreaName {
            get {
                return "News";
            }
        }

        public override void RegisterArea(AreaRegistrationContext context) {
            context.MapRoute(
                name: "News",
                url: "news/read/{news}",
                defaults: new { controller = "Home", action = "Index", news = "" },
                namespaces: new[] { "Web.Areas.News.Controllers" }
            );
            context.MapRoute(
                name: "News_default",
                url: "news/{action}",
                defaults: new { controller = "Home", action = "Index", news = "" },
                namespaces: new[] { "Web.Areas.News.Controllers" }
            );
        }
    }
}

Here is the controller

namespace Web.Areas.News.Controllers {

    public class HomeController : Controller {
        private DataModel websiteModel = new DataModel();

        //
        // GET: /News/
        [AllowAnonymous]
        public ActionResult Index(string news = "") {
            if (!websiteModel.NewsExists(news))
                return View("Index");
            else
                return View("Read", new NewsModel(websiteModel.GetNews(news.ToLower())));
        }

        [AllowAnonymous]
        public ActionResult Read(string news = "") {
            return RedirectToAction("Index", new { news = news });
        }
    }
}

When I try to access http://localhost/news/ I just get a 404. Any ideas how to fix it?

EDIT Early I had it in my RouteConfig, there it worked fine. After a small refactor, I moved the three areas I have, Home, Admin and News, in seperated Areas, now only the news don't work, the rest works fine.

The News were earlier in the HomeAreaRegistration this looked like that:

using System.Web.Mvc;

namespace Web.Areas.Home {
    public class HomeAreaRegistration : AreaRegistration {
        public override string AreaName {
            get {
                return "Home";
            }
        }

        public override void RegisterArea(AreaRegistrationContext context) {
            context.MapRoute(
                name: "News",
                url: "News/Read/{news}",
                defaults: new { controller = "News", action = "Index", news = "" },
                namespaces: new[] { "Web.Controllers" }
            );
            context.MapRoute(
                name: "Default",
                url: "{controller}/{action}/{id}",
                defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional },
                namespaces: new[] { "Web.Controllers" }
            );
        }
    }
}

EDIT 2 The page is running on my local IIS, not IIS Express and on port 80.

EDIT 3 The NewsAreaRegistration routes are all registered, I just could check it.

Upvotes: 3

Views: 1811

Answers (2)

Knerd
Knerd

Reputation: 2002

I could solve it, by this question: MVC 4 Area Routing is not working

The problem was, that the default route was registered before I registered the News_default and News routes.

I changed the code to this:

RouteConfig:

using System.Web.Mvc;
using System.Web.Routing;

namespace Web {

    public class RouteConfig {

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

            AreaRegistration.RegisterAllAreas();
            routes.MapRoute(
                name: "Default",
                url: "{controller}/{action}/{id}",
                defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional },
                namespaces: new[] { "Web.Controllers" }
            );
        }
    }
}

NewsAreaRegistration

using System.Web.Mvc;

namespace Web.Areas.News {
    public class NewsAreaRegistration : AreaRegistration {
        public override string AreaName {
            get {
                return "News";
            }
        }

        public override void RegisterArea(AreaRegistrationContext context) {
            context.MapRoute(
                name: "News",
                url: "news/read/{news}",
                defaults: new { controller = "Home", action = "Index", news = "" },
                namespaces: new[] { "Web.Areas.News.Controllers" }
            );
            context.MapRoute(
                name: "News_default",
                url: "news/{action}",
                defaults: new { controller = "Home", action = "Index", news = "" },
                namespaces: new[] { "Web.Areas.News.Controllers" }
            );
        }
    }
}

Upvotes: 1

hjavaher
hjavaher

Reputation: 2617

There are three things I see that could cause the issue you're having:

  1. (more likely) is that you forgot your port number on your URL http://localhost:portnumber]/News. you can find your port number by opening the iis express in your taskbar.
  2. iis Express is not running (Run your application in Visual Studio)
  3. Can you try to replace your news="" with news=UrlParameter.Optional and see if this is your issue?

Hope this helps

Upvotes: 0

Related Questions