0

我已经设置了我的路线,以便我拥有以下平面 URL 结构:

mywebsite/service-option-one (goes to Home Controller, Option1 action)
mywebsite/service-option-two (goes to Home Controller, Option2 action)
mywebsite/ (goes to Home Controller, Index)
mywebsite/about (goes to Home Controller, Index with path=about)
mywebsite/contact (goes to Home Controller, Index with path=contact)

这很重要,因为我有很多内容视图并且不想对这些通用信息页面进行单独的操作,解决视图的简单代码在本文末尾。

在构建菜单时 MVCHtml.ActionLink助手,但它们为通用内容提供了不正确的地址,这是有道理的,因为这些操作不存在!

给定我的地址方案,我怎么能有一个帮助方法来设置我的锚链接目标,或者我只需要求助于 html 中的硬编码(即<a href="~/contact>Contact us</a>)?

// note that the order of the routes is very important!
public static void RegisterRoutes(RouteCollection routes)
{
    routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

    // error route
    routes.MapRoute(
        "Error", // Route name
        "Oops", // URL with parameters
        new { controller = "Home", action = "Error" }
    );

    routes.MapRoute(
        "Service1",
        "service-option-one",
        new { controller = "Home", action = "Option1" }
    );

    routes.MapRoute(
        "Service2",
        "service-option-two",
        new { controller = "Home", action = "Option1" }
    );

    // default home controller route for general site content (/content), uses default path value set to Index
    routes.MapRoute(
        name: "Catchall",
        url: "{path}",
        defaults: new { controller = "Home", action = "Index", path = "Index" }
    );

    // home page route, blank url (/)
    routes.MapRoute(
        "HomePage", // Route name
        "", // URL with parameters
        new { controller = "Home", action = "Index" }
    );

    // default route
    routes.MapRoute(
        "Default", // Route name
        "{controller}/{action}/{id}", // URL with parameters
        new { controller = "{controller}", action = "{action}", id = UrlParameter.Optional } // Parameter defaults
    );
}


public class HomeController : Controller
{
    private bool ViewExists(string name)
    {
        ViewEngineResult result = ViewEngines.Engines.FindView(ControllerContext, name, null);
        return (result.View != null);
    }

    public ActionResult Index(string path)
    {
        System.Diagnostics.Debug.WriteLine("looking for..." + path);

        if (ViewExists(path)==true)
        {
            System.Diagnostics.Debug.WriteLine("general controller action...");
            return View(path);
        }
        else
        {
            System.Diagnostics.Debug.WriteLine("page not found...");
            return RedirectToRoute("Error");
        }
    }

    public ActionResult Error()
    {
        return View();
    }

    public ActionResult Option1()
    {
        return View();
    }

    public ActionResult Option2()
    {
        return View();
    }
}
4

1 回答 1

1

您正在从与使用帮助器时应采用的相反位置接近使用 ActionLink 帮助器。您试图告诉 Razor 链接的路由 url 应该是什么,而实际上这就是 Razor 将为您做的事情。因此,对于mywebsite/service-option-one的示例,您应该service-option-one写下的唯一位置是在您的路线图中。在你看来,你应该做

@Html.ActionLink("The Anchor Text You Want To Display","Option1","Home")

然后当 Razor 渲染视图时,它将转换为

<a href="mywebsite/service-option-one">The Anchor Text You Want To Display</a>

http://msdn.microsoft.com/en-us/library/dd505070(v=vs.118).aspx

于 2013-12-19T20:05:31.803 回答