Reputation: 1
So these are the HomeController.cs and RouteConfig.cs. When I tried to write the URL : localhost/Home/Index/Sometitle, the parameter is always null. The same thing happen when I wrote the URL : localhost/Home/Ajouter/SomeTitle. I already tried to find on the internet an answer but I had no success. Can someone tell me what's wrong or missing?
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 }
);
}
}
}
Upvotes: 0
Views: 222
Reputation: 21
You need to change your parameter name to Id:
public ActionResult Index(string Id)
or replace the route with this:
routes.MapRoute(
name: "CustomName",
url: "{controller}/{action}/{title}",
defaults: new { controller = "Home", action = "Index", title = UrlParameter.Optional }
);
What I really recommand is to read more about routing in MVC
Upvotes: 2
Reputation: 2896
Your route must match the controller variable name try this.
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{title}",
defaults: new { controller = "Home", action = "Index", title = UrlParameter.Optional }
);
Upvotes: 1