2017-02-07 4 views
0

これはHomeController.csとRouteConfig.csです。 URL:localhost/Home/Index/Sometitleを記述しようとすると、パラメータは常にnullになります。 localhost/Home/Ajouter/SomeTitleというURLを書いたときも同じことが起こります。私はすでにインターネット上で答えを見つけようとしましたが、私は成功しませんでした。誰かが何が間違っているか不足していると教えてくれる?なぜURLのパラメータをMVCのメソッドに直接渡すことはできません(パラメータは常にnullです)

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

using Wiki.Models.DAL; 

namespace Wiki.Controllers 
{ 
    public class HomeController : BaseController //Controller 
    { 
     Articles allArticles = new Articles(); 

     // GET: Home 
     [HttpGet] 
     public ActionResult Index(string title) 
     { 
      if (String.IsNullOrEmpty(title)) 
       return View(); 
      else 
       return RedirectToAction("Index", "Article", new { title = title }); 
     } 

     [HttpGet] 
     public ActionResult Ajouter(string title) 
     { 
      if (ModelState.IsValid) 
       return RedirectToAction("Edit", "Article", new { title = title }); 
      else 
       return View("Index"); 
     } 
    } 
} 


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

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

      //routes.MapRoute(
      // name: "Wiki", 
      // url: "Wiki/{titre}/{action}", 
      // defaults: new { controller = "Wiki", action = "Index", titre = UrlParameter.Optional } 
      //); 

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

投稿者:Ajouterへの投稿 – Steve

+0

インデックスルートが 'id'を呼び出していますが、あなたのパラメータは' title'と呼ばれています – maccettura

答えて

1

あなたのルートはコントローラの変数名と一致する必要があります。

あなたがIDに自分のパラメータ名を変更する必要が
routes.MapRoute(
     name: "Default", 
     url: "{controller}/{action}/{title}", 
     defaults: new { controller = "Home", action = "Index", title = UrlParameter.Optional } 
    ); 
2

public ActionResult Index(string Id)

またはこれにルートを置き換える:私は本当にrecommandルーティングの詳細を読むことです

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

何MVCで

関連する問題