ASP.NET MVC,电子邮件地址作为参数中断路由

时间:2011-08-23 01:31:04

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

我有以下行动结果:

  

public ActionResult Confirmation(string emailAddress)

当我尝试访问它时:

http://localhost:8080/Signup/Confirmation?emailAddress=test%40test.com

我明白了:

The view 'test@test.com' or its master was not found or no view engine supports the searched locations. The following locations were searched:
~/Views/Signup/test@test.com.cshtml
~/Views/Signup/test@test.com.vbhtml

为什么不寻找正确的观点?如果我转到“/ SignUp /”它会正确显示索引,以及其他ActionResults正常工作。为什么地址会破坏它?

2 个答案:

答案 0 :(得分:2)

您不应该在URL中传递该信息。

如果这是来自注册的“确认”页面,您可以传递另一个标识符,例如刚刚创建的UserId,然后从存储库中获取它。

E.g:

[HttpPost]
public ActionResult Signup(SignupViewModel model)
{
   //.. code to save.. etc

   return RedirectToAction("Confirmation", new { id = newUser.UserId });
}

[HttpGet]
public ActionResult Confirmation(int id)
{
   var user = repo.FindById(id);
   // map to model, etc...
   return View(model);
}

所以你的网址是(没有专门路线)

http://localhost:8080/Signup/Confirmation?id=123213

将用户的电子邮件地址放在网址中是要求将其发送垃圾邮件。

答案 1 :(得分:0)

您是否尝试在global.asax.cs中注册路线?

类似的东西:

routes.Add("confirmation", 
    new Route("Signup/Confirmation/{email}", 
    new RouteValueDictionary(new { controller = "Signup", action = "Confirmation", email = UrlParameter.Optional }), 
    new MvcRouteHandler())
);