Asp.net mvc 4 如何调用某个控制器';方法,并从查询字符串中传递参数
在我的应用程序中,我生成了如下url:Asp.net mvc 4 如何调用某个控制器';方法,并从查询字符串中传递参数,asp.net-mvc-4,query-string,actionmethod,Asp.net Mvc 4,Query String,Actionmethod,在我的应用程序中,我生成了如下url: public JsonResult MailConfirmed(string mail, string confirmCode) { try { // Here I will get user and update it in DB return Json("success", JsonRequestBehavior.AllowGet); } c
public JsonResult MailConfirmed(string mail, string confirmCode)
{
try
{
// Here I will get user and update it in DB
return Json("success", JsonRequestBehavior.AllowGet);
}
catch(Exception ex)
{
return Json("fail", JsonRequestBehavior.AllowGet);
}
}
http://www.test.com/?mail=test%40gmail.ba&code=71147ff9-87ae-41fc-b53f-5ecb3dbe5a01
我生成Url的方式如下所示:
private string GenerateUrl(string longUrl, string email, string confirmCode)
{
try
{
// By the way this is not working (Home/MailConfirmed) I'm getting message
// Requested URL: /Home/MailConfirmed
// The resource cannot be found.
string url = longUrl + "/Home/MailConfirmed";
var uriBuilder = new UriBuilder(url);
var query = HttpUtility.ParseQueryString(uriBuilder.Query);
query["mail"] = email;
query["code"] = confirmCode;
uriBuilder.Query = query.ToString();
uriBuilder.Port = -1;
url = uriBuilder.ToString();
return url;
}
catch (Exception ex)
{
return "Error happened: " + ex.Message;
}
}
在longUrl中我通过了www.test.com,在电子邮件中我通过了
test@gmail.com等等
有关于我的网站的信息:
www.test.com
邮寄:test@gmail.com
确认代码:71147ff9-87ae-41fc-b53f-5ecb3dbe5a01
在myHomeController.cs
中有一种方法,它应该从查询字符串-url中提取参数,并将其传递给应该通过邮件获取用户(邮件是唯一的)并将此guid与数据库中的guid进行比较来激活用户帐户的方法。所以我想知道如何调用这个方法
因此,我的方法如下所示:
public JsonResult MailConfirmed(string mail, string confirmCode)
{
try
{
// Here I will get user and update it in DB
return Json("success", JsonRequestBehavior.AllowGet);
}
catch(Exception ex)
{
return Json("fail", JsonRequestBehavior.AllowGet);
}
}
所以我的问题是,用户如何才能点击下面的链接并调用my方法
非常感谢
干杯为了导航到您的
邮件确认()
,您的url需要
http://www.test.com/Home/MailConfirmed?mail=test%40gmail.ba&confirmcode=71147ff9-87ae-41fc-b53f-5ecb3dbe5a01
注意控制器和动作名称的段,并且code=xxx
应为confirmcode=xxx
,以匹配方法中的参数名称
通过使用UrlHelper
方法生成url,您可以简化代码(并删除GenerateUrl()
方法)
要生成上述url,控制器方法中所需的全部内容是
string url = Url.Action("MailConfirmed", "Home",
new { mail = email, confirmcode = confirmCode },
this.Request.Url.Scheme);
您的url必须是
http://www.test.com/Home/MailConfirmed?mail=test%40gmail.ba&confirmcode=71147ff9-87ae-41fc-b53f-5ecb3dbe5a01
@Stephenmueecke所以在UriBuilder中我应该写一个www.test.com/Home/mailconfixed+parameters?:您为什么要使用UriBuilder
(而不是使用Url.Action()
)?@StephenMuecke我需要发送一个Url,该Url应该将用户重定向到激活其帐户的方法,所以我想这就是方法。。你想让我发布我如何创建Url的完整代码,以便我们可以编辑它,你可以将其作为答案发布,以便我可以接受它?当然可以(通过发送url,您的意思是向具有链接的用户发送电子邮件吗?)