0

I have to make custom route but I stuck on a problem. That is my route

routes.MapRoute(
            name: "IndexByUserName",
            url: "{controller}/{action}/{username}",
            defaults: new { controller = "Profile", action = "Edit", username = UrlParameter.Optional }
        );

And when I go to url .../Profile/Edit/UserTest for example I get 404 Not Found error, because my parameter username is null. My action looks like this

    [Authorize]
    [HttpGet]
    public ActionResult Edit(string username)
    {

        ApplicationUser profile = db.Users.Find(username);
        if (profile == null)
        {
            return HttpNotFound();
        }
        return View(profile);
    }

    [Authorize]
    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Edit([Bind(Include = "Id, Nickname, FirstName, LastName, SecondName, City, Address, Description, Skype, TelephoneNum")] ApplicationUser profile)
    {
        if (ModelState.IsValid)
        {
            var user = db.Users.Find(profile.Id);
            if (user == null)
            {
                return HttpNotFound();
            }
            user.UserName = User.Identity.GetUserName();
            user.FirstName = profile.FirstName;
            user.SecondName = profile.SecondName;
            user.LastName = profile.LastName;
            user.SocialNetworks = profile.SocialNetworks;
            user.Address = profile.Address;
            user.City = profile.City;
            user.TelephoneNum = profile.TelephoneNum;
            user.Description = profile.Description;
            db.Entry(user).State = EntityState.Modified;
            db.SaveChanges();
            return Redirect("/Profile/Index/" + User.Identity.Name);
        }
        return View(profile);
    }

I don't know where the problem.

2 Answers2

1

Have you removed the default route? Your route looks like it will have the same default specificity, so if the default route appears first, your application will just use that one.

See this answer for how to resolve this issue (if that's your issue).

Community
  • 1
  • 1
Kolichikov
  • 2,944
  • 31
  • 46
  • I am not removing the default root. – Dimitar Ganichev Aug 20 '16 at 08:43
  • You don't have to. But since you aren't removing the default route, the reason your route isn't working is probably because the default is being picked up instead of yours. The answer I linked to specifies how to have your route be used without removing the default route. – Kolichikov Aug 20 '16 at 15:50
  • I saw the link that you put in your answer but when I put my custom route before the Default one nothings happen. :( – Dimitar Ganichev Aug 21 '16 at 10:42
  • Did you modify the route to be more specific as well? I was able to get it to select your route by adding it first to the RouteConfig, changing the url to be `url: "Profile/{action}/{username}", defaults: new { controller = "Profile", action = "Edit", username = UrlParameter.Optional }`. I then verified that accessing .../Profile/Edit/Test1 gave me username as Test1. As an additional debug step, you can try to override `BeginExecute` in your ProfileController and look at the requestContext.RouteData variable to see which Route is being used. – Kolichikov Aug 21 '16 at 19:08
  • When I edited my custom route like that `url: "Profile/{action}/{username}", defaults: new { controller = "Profile", action = "Edit", username = UrlParameter.Optional }` the problem disappear. Thank you @Kolichkov! – Dimitar Ganichev Aug 21 '16 at 20:52
0

In the RegisterRoute method of RouteConfig.cs file in the App_Start folder of your project where Default Routing rule is written there Default route rule is :-ControllerName/ActionName/id

where id is optional. for ex-

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

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

}

so you want to make it custom make userName = " " in your routing rule.

and in your ViewPage on button click event use can jquery

$("#Selector").click(function(response){
$.get("@Url.Action("ControllerName","ActionName)",{userName:"Name of User"})

});

where selector means Id of Edit button tag.