【问题标题】:Redirect Url To Another Action Method将 URL 重定向到另一个操作方法
【发布时间】:2016-01-23 11:52:19
【问题描述】:

我正在做通知模块。下面的代码显示了通知表模型

public class Notification
{
    [Key]
    public int NotificationID { get; set; }
    .
    .
    [Required]
    public bool Active { get; set; }
}

当用户点击通知时,bool Active 应设置为 false,然后重定向到 Url 列中指定的 url。我已经通过 signalR 完成了通知,但是单击通知重定向到 url 没有完成。我无法重定向到指定的网址。

【问题讨论】:

  • 向我们展示您的 jqyery 代码。
  • 点击通知后,请求将连同通知 id 一起发送到控制器,在那里处理所有内容,然后重定向到指定的 url。它不在 jquery 中处理。
  • 好的,给我们看看你有什么代码,除非我们看到你的代码,否则我们无法解决你的问题。
  • public ActionResult NotificationRedirect(int NotificationID) { var notification = db.Notifications.Find(NotificationID); ....返回重定向(notification.Url); }

标签: asp.net redirect asp.net-mvc-5 notifications url-redirection


【解决方案1】:

您可以在用户单击通知项时执行的操作方法中更新您的表记录。

public ActionResult NotificationRedirect(int notificationId) 
{ 
   var notification = db.Notifications.FirstOrDefault(notificationId);
   if(notification!=null)
   {
       notification.Active=false;
       db.Entry(notification).State = EntityState.Modified;
       db.SaveChanges();
       return Redirect(notification.Url); 
   } 
   return View("NotificationNotFound"); //make sure you have a view with this name
}

现在从您的客户端,您需要使用锚标记构建标记(当您接到来自 singlaR 的呼叫时),其中您的 href 值设置为带有 @ 的 NotificationRedirect 操作方法987654324@ 查询字符串。类似的东西

<a href="YourControllerName/NotificationRedirect?notificationId=123">Notification tex</a>

此外,对于重定向,如果您要显示应用程序内部的页面,您也可以考虑使用RedirectToAction 方法。

【讨论】:

  • 谢谢。但返回 Redirect(notification.Url);无法正常工作。然后在notification.url前面加上"../" return Redirect("../"+ notification.Url);现在它可以正常工作了
  • Redirect 方法需要一个绝对 URL(例如:http://www.google.com)。为什么要在 url 之前附加 "../" ?如果您尝试重定向到应用程序中的页面/视图,则应考虑使用RedirectToAction 方法。
猜你喜欢
  • 1970-01-01
  • 2016-01-10
  • 1970-01-01
  • 2013-05-28
  • 2020-09-05
  • 2016-05-24
  • 2021-11-21
  • 1970-01-01
  • 2013-08-04
相关资源
最近更新 更多