在我的 MVC 应用程序中,我从 RabbitMQ 队列中获取了一个事件。开始处理事件
public void Consume()
{
ConnectionFactory factory = new ConnectionFactory
{
//..
};
IConnection connection = factory.CreateConnection();
IModel channel = connection.CreateModel();
channel.QueueDeclare("MyQueueName", true, false, false, null);
EventingBasicConsumer consumer = new EventingBasicConsumer(channel);
consumer.Received += OnReceived;
channel.BasicConsume("MyQueueName", true, consumer);
}
Run Code Online (Sandbox Code Playgroud)
处理我使用的事件
private async void OnReceived(object model, BasicDeliverEventArgs deliverEventArgs)
{
var info = Encoding.UTF8.GetString(deliverEventArgs.Body);
var model = JsonConvert.DeserializeObject<MyModel>(info);
if (model != null)
{
await _groupServiceProvider.GetService<IMyService>().ProcessEvent(model);
}
}
Run Code Online (Sandbox Code Playgroud)
最后,为了处理/更新我使用的模型
public class MyService : IMyService
{
private readonly IHostingEnvironment _env;
public …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用从另一个控制器调用方法RedirectToAction()。但这是行不通的。您能解释一下我在做什么错吗?
[HttpPost]
public ActionResult AddToWishList(int id, bool check)
{
var currentUser = WebSecurity.CurrentUserId;
if (currentUser != -1)
{
// ...
}
else
{
return RedirectToAction("Login", "Account");
}
}
Run Code Online (Sandbox Code Playgroud)
我在HTML中调用该方法:
<script>
$(document).ready(function () {
/* call the method in case the user selects a checkbox */
$("#checkbox".concat(@Model.Id)).change(function () {
$.ajax({
url: '@Url.Action("AddToWishList", "Item")',
type: 'POST',
data: {
id: '@Model.Id',
check: this.checked
}
});
});
});
Run Code Online (Sandbox Code Playgroud)
如果我使用它会起作用:
success: function (result) {
window.location.href = "@Url.Content("~/Account/Login")";
}
Run Code Online (Sandbox Code Playgroud)
但是,只有在未授权用户的情况下,我不需要在每次单击后导航到Login()。您能否解释一下如何在控制器中使用重定向?