【问题标题】:Redirect using routing in MVC在 MVC 中使用路由重定向
【发布时间】:2014-06-16 21:29:18
【问题描述】:

我有 ASP.NET MVC 应用程序。 我希望我的应用程序从

重定向

example.com/Register

example.com/Account/Register

我怎样才能用路线做到这一点?只为这一项任务制作控制器对我来说毫无意义

public class RegisterController : Controller
{
    public ActionResult Index()
    {
        return RedirectToAction("Register", "Account");
    }
}

【问题讨论】:

标签: asp.net asp.net-mvc asp.net-mvc-4 asp.net-mvc-5 asp.net-mvc-routing


【解决方案1】:

您不需要重定向。您需要自定义路线

先添加这条路线(高于“默认”)

routes.MapRoute(
                    "Register",
                    "Register",
                    new { controller = "Account", action = "Register" }
                );

此解决方案将用户留在 URL example.com/Register,但实例化 Controller Account,执行 ActionResult Register,并返回 View Account/Register。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-09-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-18
    相关资源
    最近更新 更多