路由不适用于剃须刀

时间:2014-02-11 06:06:29

标签: asp.net-mvc asp.net-mvc-4 razor

我的razor mvc 4网站路由有问题。

Newssite位于自定义区域,此处为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" }
            );
        }
    }
}

这是控制器

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

当我尝试访问http://localhost/news/时,我只获得了404.任何想法如何解决?

编辑我早些时候在RouteConfig收到了它,它运行良好。在一个小型的重构之后,我将我所拥有的三个区域,Home,Admin和News移到了分离的区域,现在只有新闻不起作用,其余的工作正常。

新闻早些时候HomeAreaRegistration看起来像这样:

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

编辑2 该页面在我的本地IIS上运行,而不是在IIS Express和端口80上运行。

编辑3 NewsAreaRegistration路线全部注册,我只是可以查看。

2 个答案:

答案 0 :(得分:1)

我可以通过这个问题解决它:MVC 4 Area Routing is not working

问题是,默认路线是之前注册我注册了News_defaultNews路线。

我将代码更改为:

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

答案 1 :(得分:0)

我发现有三件事可能导致你遇到的问题:

  1. (更有可能)是您忘记了网址http://localhost:portnumber]/News上的端口号。您可以通过在任务栏中打开iis express来找到您的端口号。
  2. iis Express未运行(在Visual Studio中运行您的应用程序)
  3. 您是否可以尝试将news=""替换为news=UrlParameter.Optional并查看这是否是您的问题?
  4. 希望这有帮助