C# Asp.Net MVC - 仅参数操作请求

C# Asp.Net MVC - Parameter Only Action Request

我有一个 Asp.Net MVC 项目,我们允许我们的用户拥有 public 个配置文件。

我想改进url,让它更友好,更简短。

现有代码如下-

public class ProfileController : Controller
    {
        private readonly IUserProfileService _userProfileService;

        public ProfileController(IUserProfileService userProfileService)
        {
            this._userProfileService = userProfileService;
        }

        public ActionResult Index(string id)
        {
            //Get users profile from the database using the id
            var viewModel = _userProfileService.Get(id);

            return View(viewModel);
        }
    }


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

        //Required for the route prefix attributes to work!
        routes.MapMvcAttributeRoutes();

        routes.MapRoute(
            "ProfileUrlIndexActionRemoval",
            "Profile/{id}",
            new { controller = "Profile", action = "Index" }
        );

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

上述代码允许以下 url 工作(基于默认 MVC 路由)- www.mydomain.com/profile/john-doe

为了让下面的 url 起作用,我需要实施什么路由 - www.mydomain.com/john-doe

谢谢。

这就是我要做的。注册一个匹配根斜杠后任意字符串的路由。

请注意,这会严重限制您可以用于应用程序的路由,因为并非所有匹配 /{id} 的东西实际上可能都是用户 ID,这就是为什么应用程序通常会在路由前加上 /profile 或前缀/p.

public static void RegisterRoutes(RouteCollection routes)
{
    routes.MapRoute(
        name: "UserIdRoute",
        url: "{id}",
        defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
    );

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

public ActionResult Index(string id)
{
    //Get users profile from the database using the id
    var viewModel = _userProfileService.Get(id);

    return View();
}

这有点棘手,因为您希望站点根目录中的友好 URL 不与任何其他路由冲突。

这意味着,如果您有任何其他路线,例如“关于”或“联系”,您需要确保它们位于路线 table 之前的友好路线,以避免路线冲突。

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

        //Required for the route prefix attributes to work!
        routes.MapMvcAttributeRoutes();

        routes.MapRoute(
            "ProfileUrlIndexActionRemoval",
            "Profile/{id}",
            new { controller = "Profile", action = "Index" }
        );

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

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

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

        routes.MapRoute(
            "Default_Frieldly",
            "{*id}",
            new { controller = "Profile", action = "Index" }
        );
    }
}

最后,因为默认路由将捕获所有不匹配的路由,您需要将未找到的配置文件考虑在内。

public class ProfileController : Controller {

    //...code removed for brevity

    public ActionResult Index(string id) {
        //Get users profile from the database using the id
        var viewModel = _userProfileService.Get(id);

        if(viewModel == null) {
            return NotFound();
        }

        return View(viewModel);
    }
}

通过在原始 URL 中使用配置文件控制器前缀,它使其变得独一无二,以避免路由冲突,但在想要 root 友好 URL 时,虽然并非不可能,但你会看到箍需要跳过才能获得所需的行为。