【问题标题】:ASP.NET MVC, email address as parameter breaking routesASP.NET MVC,电子邮件地址作为参数中断路由
【发布时间】:2011-08-23 01:31:03
【问题描述】:

我有以下操作结果:

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/”,它会正确显示索引,以及其他正常工作的 ActionResult。为什么一个地址会破坏它?

【问题讨论】:

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


【解决方案1】:

无论如何,您都不应该在 URL 中传递该信息。

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

例如:

[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);
}

所以你的 URL 将是(没有专门的路由)

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

将用户的电子邮件地址放在 URL 中会要求他们发送垃圾邮件。

【讨论】:

  • 我不确定用户会如何收到垃圾邮件。垃圾邮件发送者如何在抓取网站时通过 URL 读取任何其他用户的电子邮件地址?我想中间的人可能会拦截请求并捕获 url,但这似乎是获取电子邮件地址的一种非常密集的方式。
  • @Mystere Man - 这是可能的。让我们不要进入它,但一般来说,总是最好不在可能的情况下传递此信息,在这种情况下肯定是可能的。不添加任何值以在 URL 中传递电子邮件。
【解决方案2】:

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

类似:

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

【讨论】:

    猜你喜欢
    • 2021-06-22
    • 2011-05-17
    • 1970-01-01
    • 1970-01-01
    • 2019-01-24
    • 2016-06-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多