0

Dont't vote Negative if cant solve the prob because i know what you answer thats why iam here at the end.

Controller

[Route("{Name}")]
    public ActionResult Details(int? id, string Name)
    {          

        if (id == null)
        {
            return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
        }
        Menu menu = db.Menus.Find(id);
        if (menu == null)
        {
            return HttpNotFound();
        }
        return View(menu);
    }

Views

@Html.ActionLink("Details", "Details", new { id = item.Id, Name = item.MenuName })

Route.config.cs

route.MapMvcAttributeRoutes();

Output:

how to get output like this

localhost:2345/Blog instead of localhost:2345/Details/id=1?Name=Blog

saurav singh
  • 438
  • 6
  • 16
  • You cannot get just `../Blog` (that url would be calling the `Index()` method of `BlogController` assuming your using the default routes). Routes need to be distinguishable. And in any case you also seem to be wanting to pass a value for `id` –  Jul 26 '17 at 08:23
  • Yes, i am using default routes .how can i achieve it i want to create a dynamic page .so just tell me how will i get this . even changes in route.config.cs or whatever . – saurav singh Jul 26 '17 at 08:27
  • Did you not read my comment - you cannot. And your code makes no sense. Why pass a value for `Name` when you never even use it. And because a value for the `id` will always be `null` (because its not included in the url), then your code will always return `BadRequest` which is a bit pointless. –  Jul 26 '17 at 08:31
  • My best guess is that you really want a slug route (e.g. as per SO routes), so that you get .../Details/1/Blog` in which case refer [this answer](https://stackoverflow.com/questions/30349412/how-to-implement-url-rewriting-similar-to-so/30363600#30363600) –  Jul 26 '17 at 08:32
  • Do you have any example how to get dynamic pages from database. with url like localhost:2345/page – saurav singh Jul 26 '17 at 08:34

2 Answers2

1

You can't because the url in the RouteConfig has a specific format: {controller}/{action}/{id}

To get the url you want, you may create public ActionResult Blog()

public class RouteConfig
{
    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
        routes.MapRoute(
            name: "NoScript",
            url : "noscript",
            defaults : new { controller = "Home", action = "EnableJavaScript"}
            );
        routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",//the specific format
            defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
        );
    }
}
Indrit Kello
  • 1,293
  • 8
  • 19
  • i am creating dynamic page just not specific blog i am fetching table using scalffoding andin view part like localhost:2345/Name which i want – saurav singh Jul 26 '17 at 08:42
  • https://our.umbraco.org/forum/developers/api-questions/21372-How-to-dynamically-generate-pages-from-a-database-table This article can be helpful – Indrit Kello Jul 26 '17 at 08:57
  • you dont understand my question, i am saying suppose i have Home controller and i have database list in Index and i want to show that category or list on click details or any view to go through with the localhost:2345/listName – saurav singh Jul 26 '17 at 09:03
0

Assuming you have a UserController with the following methods

// match http://..../arrivaler
    public ActionResult Index(string username)
    {
        // displays the home page for a user
    }

    // match http://..../arrivaler/Photos
    public ActionResult Photos(string username)
    {
        // displays a users photos
    }

Route.config.cs

public class RouteConfig
{
    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
        routes.MapRoute(
            name: "User",
            url: "{username}",
            defaults: new { controller = "User", action = "Index" },
            constraints: new { username = new UserNameConstraint() }
        );
        routes.MapRoute(
            name: "UserPhotos",
            url: "{username}/Photos",
            defaults: new { controller = "User", action = "Photos" },
            constraints: new { username = new UserNameConstraint() }
        );
        routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "Test", action = "Index", id = UrlParameter.Optional }
        );
    }

    public class UserNameConstraint : IRouteConstraint
    {
        public bool Match(HttpContextBase httpContext, Route route, string parameterName, RouteValueDictionary values, RouteDirection routeDirection)
        {
            List<string> users = new List<string>() { "Bryan", "Stephen" };
            // Get the username from the url
            var username = values["username"].ToString().ToLower();
            // Check for a match (assumes case insensitive)
            return users.Any(x => x.ToLower() == username);
        }
    }
}

If the url is .../Arrivaler, it will match the User route and you will execute the Index() method in UserController (and the value of username will be "Arrivaler")

If the url is .../Arrivaler/Photos, it will match the UserPhotos route and you will execute the Photos() method in UserController (and the value of username will be "Arrivaler")

Note that the the sample code above hard codes the users, but in reality you will call a service that returns a collection containing the valid user names. To avoid hitting the database each request, you should consider using MemoryCache to cache the collection. The code would first check if it exists, and if not populate it, then check if the collection contains the username. You would also need to ensure that the cache was invalidated if a new user was added.

saurav singh
  • 438
  • 6
  • 16