1

次の URL でユーザーの詳細を表示しようとしています。

www.website.com/users/yasser

最後のエントリyasserはユーザー名です。いくつかのルートを試しましたが、うまくいきません。

私のユーザーコントローラーは以下のとおりです。

public class UserController : Controller
{
    public ActionResult Index(string username)
    {
        var model = _service.GetUserDetails(username);
        return View(model);
    }
}

これと他のいくつかのリンクを参照しましたが、それがどのように機能するかを本当に理解できませんでした.

誰かがこれについて私を助けることができますか?ありがとう

編集

私の現在のルート設定は以下です

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

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

1 に答える 1

7

ルートは上から下に実行されます。

routes.MapRoute("UserProfile",
     "Users/{username}",
     new { controller = "User", action = "Index", username = string.Empty }
);

routes.MapRoute("Default",
     "{controller}/{action}/{id}",
     new { controller = "Home", action = "Index", id = UrlParameter.Optional}
);
于 2012-11-03T07:43:45.490 に答える