2011-09-30 6 views
0

フロントエンド用のページを作成/編集/削除する機能が自分のサイトにあります。ここに私のコントローラです:動的コンテンツページへのHtml.ActionLinkの作成

namespace MySite.Controllers 
{ 
    public class ContentPagesController : Controller 
    { 
     readonly IContentPagesRepository _contentPagesRepository; 

     public ContentPagesController() 
     { 
      MyDBEntities entities = new MyDBEntities(); 
      _contentPagesRepository = new SqlContentPagesRepository(entities); 
     } 


     public ActionResult Index(string name) 
     { 
      var contentPage = _contentPagesRepository.GetContentPage(name); 

      if (contentPage != null) 
      { 
       return View(new ContentPageViewModel 
       { 
        ContentPageId = contentPage.ContentPageID, 
        Name = contentPage.Name, 
        Title = contentPage.Title, 
        Content = contentPage.Content 
       }); 
      } 

      throw new HttpException(404, ""); 
     } 
    } 
} 

そして、私のglobal.asaxで:

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

    routes.MapRoute(
     "Page", // Route name 
     "Page/{name}", // URL with parameters 
     new { controller = "ContentPages", action = "Index" }, // Parameter defaults 
     new[] { "MySite.Controllers" } 
    ); 

    routes.MapRoute(
     "Default", // Route name 
     "{controller}/{action}/{id}", // URL with parameters 
     new { controller = "Home", action = "Index", id = UrlParameter.Optional }, // Parameter defaults 
     new[] { "MySite.Controllers" } 
    ); 
} 

だから私は約という名前の、私のデータベースに動的なページを持っています。 mysite.com/Page/Aboutに行くと、私は動的コンテンツを見ることができます。

このページへのアクションリンクを作成します。私はこのようにそれを試してみた:

@Html.ActionLink("About Us", "Index", "ContentPages", new { name = "About" }) 

しかし、私はページ上のリンクを見たときに、urlはちょうどクエリ文字列にLength=12と、現在のページに移動します。たとえば、私がホームページにいる場合、リンクはmysite.com/Home?Length=12

ここで間違っていますか?

答えて

2

正しいActionLinkオーバーロードを使用していません。このようにしてみてください。

@Html.ActionLink(
    "About Us",    // linkText 
    "Index",     // action 
    "ContentPages",   // controller 
    new { name = "About" }, // routeValues 
    null      // htmlAttributes 
) 

のに対し、あなたの例では:あなたが期待されるリンクを生成しない理由はかなり明らかに説明している

@Html.ActionLink(
    "About Us",    // linkText 
    "Index",     // action 
    "ContentPages",   // routeValues 
    new { name = "About" } // htmlAttributes 
) 

関連する問題