1

I just want to customize routes in asp.net mvc , This is a blog website and I want to access controller methods using

wwww.sitename.com/blog/{blogtitle}

www.sitename.com/blog/{action}

Blog Controller

 public class BlogController : Controller
{

  public ActionResult Index(string title)
    {
        return View();
    }



        [Route("post-blog")]
    [HttpPost]
    public ActionResult Post(Blog blog,HttpPostedFileBase blogimage)
    {

        //some coe

    }
    [Route("post-blog")]
    public ActionResult Post()

    {

        if (Request.Cookies["userInfo"]==null)
        {
            return Redirect("/login");
        }

        return View();
    }

}

Here is route Config

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




        routes.MapMvcAttributeRoutes();

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


  }

But I am unable to hit action name "Index" using www.site.com/blog/titlename But I can access "post-blog" using www.site.com/blog/post-blog

Please help me I am beginner in asp.net mvc routing.

1 Answer 1

1

Add your parameter to the route attribute within {} brackets to indicate that it should be read from the URL, and not from something else (such as POST body, dependency injections, etc)

[Route("{title}")]
public ActionResult Index(string title)
{
    return View();
}

I also like to add the RoutePrefix attribute to the controller to make it a bit clearer.

[RoutePrefix("blog")]
public class BlogController : Controller
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.