c# - Url action null object asp.net mvc -
i need create global url , send email. create global url url.action url.action return me null. , don't know why. knows? have method:
public string sendrecoverymessage(users usermodel) { messagemodel message = new messagemodel(); string resetlink = "<a href='" + url.action("setnewpassword", "home", new { timelink = usermodel.timelink }, "http") + "'>Ссылка для восстановления пароля</a>"; message.emailto = usermodel.email; message.emailfrom = "narkom@info.by"; message.title = "Восстановление пароля"; message.message = resetlink; string responsetostring = sendmessage(message); try { rootobject r = jsonconvert.deserializeobject<rootobject>(responsetostring); return r.msg + "Для продолжения изменения пароля пройдите по ссылке указаной в письме. У вас есть сутки на изменение вашего пароля!"; } catch (exception) { return responsetostring; } }
and route:
public static void registerroutes(routecollection routes) { routes.ignoreroute("{resource}.axd/{*pathinfo}"); //defaulf route routes.maproute( name: "default", url: "{controller}/{action}/{id}", defaults: new { controller = "home", action = "index", id = urlparameter.optional } ); }
and recovery password method:
public actionresult setnewpassword(string timelink) { var recemaildetails = dbmodel.users.where(x => x.timelink == timelink).firstordefault(); if (recemaildetails == null) { viewbag.redirectmessage = "Данная ссылка больше не активна!"; return view("~/views/home/redirectpage.cshtml"); } datetime datenow = convert.todatetime(datetime.now); datetime dateconfirm = convert.todatetime(recemaildetails.datetime); timespan diff = dateconfirm - datenow; var totalseconds = math.floor(diff.totalseconds); totalseconds += 86400; if (totalseconds < 0) { viewbag.redirectmessage = "Время действия ссылки истекло. Нам очень жаль :c"; return view("~/views/home/redirectpage.cshtml"); } else { session["idresetuser"] = recemaildetails.id; changepasswordmodel cpm = new changepasswordmodel(); try { recemaildetails.datetime = null; recemaildetails.timelink = null; dbmodel.savechanges(); modelstate.clear(); } catch (dbentityvalidationexception exc) { foreach (var entityvalidationerrors in exc.entityvalidationerrors) { foreach (var validationerror in entityvalidationerrors.validationerrors) { viewbag.redirectmessage = exc.message; } } } return view("~/views/home/setnewpassword.cshtml", cpm); } }
try -
string resetlink = "<a href='" + url.action("setnewpassword", "home", new system.web.routing.routevaluedictionary(new { timelink = usermodel.timelink }), "http", , request.url.host) + "'>Ссылка для восстановления пароля</a>";
Comments
Post a Comment