URL解析 - 路由还是其他?

时间:2013-10-29 11:36:03

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

我正在将PHP代码迁移到ASP.NET MVC,之前我将存储的注册页面如果新用户已经接受了规则,并且通过从/register重定向到{{{}}来验证COPPA 1}}。然后我会在代码中解析@readrules和@coppa。

在ASP.NET中执行此操作的最佳方法是什么?感谢

1 个答案:

答案 0 :(得分:1)

改为使用查询字符串参数:

/register?readrules=1&coppa=1

这是更标准的,您不需要任何解析。只需定义一个视图模型来容纳这些值:

public class MyViewModel
{
    public int Readrules { get; set; }
    public int Coppa { get; set; }
}

和十个让您的Register控制器操作将此视图模型作为参数:

public ActionResult Register(MyViewModel model)
{
    ... at this stage model.Readrules and model.Coppa will contain the values passed
        as query string parameters tat you could use here
}

默认模型绑定器会自动将readrulescoppa查询字符串参数的值绑定到控制器操作所采用的视图模型的相应属性。