mvc 5中的自定义路由 [英] custom routing in mvc 5

查看:123
本文介绍了mvc 5中的自定义路由的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好我想在我的应用程序中实现自定义路由我需要

注册后用户登录URL应该是



domainname / username / login



我试试下面的代码,但没有工作,



Hi I want to implement a custom routing in my application where I want
after signup a user login url should be like

domainname/username/login

I try bellow code ,but not working,

routes.MapRoute(
               name: "Default1",
               url: "{comp}/Login",
               defaults: new { controller = "Login", action = "Index", }
           );





请帮帮我。



please help me to this.

推荐答案

因为这不是在ASP.NET中实现URL路由的正确方法。当您创建URL时,这样做,



Because this is not the correct way to implement URL Routing in ASP.NET. When you create URLs you do so this way,

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





由于Login是控制器,我在其位置添加了 {controller} ,常量从未被解析并映射到任何代码隐藏逻辑,例如Controller,Action或其他参数。控制器,操作或任何其他ID或其他参数的参数在 {} 块中传递。您无法传递自己的变量或常量文字。如果你愿意,那么你也应该在你的服务器上接受它们;比如id。它将被传递给你的行动,





Since Login is controller, I have added {controller} in its place, constants are never resolved and mapped to any code-behind logic such as Controller, Action or other parameters. The parameters for controller, action or any other ID or other parameters are passed in {} blocks. You cannot pass your own variables or constant literals. If you want to, then you should accept them on your server also; such as id. It would be passed to your action as,

public ActionResult Login(int id) { /* code */ }





您可以以非常类似的方式传递自己的变量;例如用户名字符串。



You can pass your own variables in a very similar manner; such as username string.


我不知道MVC 5中的新功能属性路由我在这里找到了一篇好文章http://blogs.msdn.com/b/webdev/archive /2013/10/17/attribute-routing-in-asp-net-mvc-5.aspx [ ^ ]
I am not aware of new feature in MVC 5 Attribute Routing I found a good article here http://blogs.msdn.com/b/webdev/archive/2013/10/17/attribute-routing-in-asp-net-mvc-5.aspx[^]


这篇关于mvc 5中的自定义路由的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆