无法创建@ Html.Routelink

时间:2016-07-20 13:07:08

标签: c# html asp.net-mvc

我创建了一个HomeController:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Experiment1.Controllers
{
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            return View();
        }
    }
}

带有索引操作方法的视图:

<!DOCTYPE html>
<html>
<head>
    <link  type="text/css" rel="stylesheet" href="~/Content/stylesheet.css"/>
    <title>My Photo Page</title>
</head>
<body>
   @Html.RouteLink("Go","Menu")
</body>
</html>

和一个带View的MenuController:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Experiment1.Controllers
{
    public class MenuController : Controller
    {
        public ActionResult Index()
        {
            return View();
        }
    }
}

查看:

@{
    ViewBag.Title = "Index";
}

<h2>SomeText</h2>

以下是RouteConfig.cs的代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;

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

            routes.MapRoute(
                name: "Default",
                url: "{controller}/{action}/{id}",
                defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
            );
            routes.MapRoute(
             name: "Menu",
             url: "{controller}/{action}",
             defaults: new { controller = "Menu", action = "Index"}
         );
        }
    }
}

但是当我在HomeController视图中使用@Html.RouteLink("Go","Menu")时,它会转到HomeController而不是MenuController。

我该如何解决这个问题?谢谢你的帮助。

0 个答案:

没有答案