What I am trying to achieve:
我想要实现的目标:
-
After each view has finished executing I would like to make a separate http call to an external partner.
在每个视图完成执行后,我想对外部合作伙伴进行单独的http调用。
-
I need to pass one of the view's content as body of that http call.
我需要传递一个视图的内容作为该http调用的主体。
What I have so far:
到目前为止我所拥有的:
I have a base controller from which all of my controllers inherit from.
我有一个基本控制器,我的所有控制器都从中继承。
I have found that i can override the onActionExecuted() method of the base controller and write my partner http call code there so that it will be executed after each action.
我发现我可以覆盖基本控制器的onActionExecuted()方法并在那里编写我的伙伴http调用代码,以便在每次操作后执行它。
I have written a custom result after reading the article at Send asp.net mvc action result inside email. which enables me to grab the content of the view. (which is part of another controller that also inherits from base controller).
我在阅读发送asp.net mvc动作结果的文章后写了一个自定义结果。这使我能够抓取视图的内容。 (它是另一个也从基本控制器继承的控制器的一部分)。
What I can't figure out:
我无法弄清楚:
- How do I make a call to the controller action (the one that will render the content for the http calls body) to get the content in my base controller onActionExecuted() method?
如何调用控制器操作(将为http调用主体呈现内容的操作)来获取基本控制器onActionExecuted()方法中的内容?
anil
3 个解决方案
#1
1
This will call a second controller action from the first controller action within the same controller:
这将从同一控制器中的第一个控制器操作调用第二个控制器操作:
public ActionResult FirstAction()
{
// Do FirstAction stuff here.
return this.SecondAction(ArgumentsIfAny);
}
public ActionResult SecondAction()
{
// Do SecondAction stuff here.
return View();
}
Doesn't need to be too complicated. :-)
不需要太复杂。 :-)
#2
0
The idea of most MVC frameworks it to make things more simple. Everything breaks down into a call to a method with certain inputs and with certain return values. In a way, you can accomplish what you want by doing something like this:
大多数MVC框架的想法让它变得更简单。一切都分解为对具有某些输入和某些返回值的方法的调用。在某种程度上,您可以通过执行以下操作来完成您想要的任务:
class MyController {
public ActionResult Action1() {
// Do stuff 1
}
public ActionResult Action2() {
// Do stuff 2
}
}
You can then refactor a bit:
然后你可以重构一下:
class MyController {
public ActionResult Action1() {
// Pull stuff out of ViewData
DoStuff1(param1, param2, ...);
}
public ActionResult Action2() {
DoStuff2(param1, param2, ...);
}
public void DoStuff1(/* parameters */) {
// Do stuff 1
}
public void DoStuff2(/* parameters */) {
// Do stuff 2
}
}
Now you can just call DoStuff1() and DoStuff2() directly because they're just methods. You can make them static if possible. Don't forget that you'll likely need to do something about error checking and return types.
现在你可以直接调用DoStuff1()和DoStuff2(),因为它们只是方法。如果可能,您可以将它们设为静态。不要忘记您可能需要对错误检查和返回类型执行某些操作。
#3
0
protected override void OnActionExecuted(ActionExecutedContext filterContext)
{
base.OnActionExecuted(filterContext);
this.ViewData.Model = GLB_MODEL;
Stream filter = null;
ViewPage viewPage = new ViewPage();
viewPage.ViewContext = new ViewContext(filterContext.Controller.ControllerContext, new WebFormView("~/Views/Customer/EmailView.aspx", ""), this.ViewData, this.TempData);
var response = viewPage.ViewContext.HttpContext.Response;
response.Clear();
var oldFilter = response.Filter;
try
{
filter = new MemoryStream();
response.Filter = filter;
viewPage.ViewContext.View.Render(viewPage.ViewContext, viewPage.ViewContext.HttpContext.Response.Output);
response.Flush();
filter.Position = 0;
var reader = new StreamReader(filter, response.ContentEncoding);
string html = reader.ReadToEnd();
}
finally
{
if (filter != null)
{
filter.Dispose();
}
response.Filter = oldFilter;
}
}
This is modified version of code from Render a view as a string. I didn't want to render the result of the view to the httpcontext response stream.
这是将视图渲染为字符串的代码的修改版本。我不想将视图的结果呈现给httpcontext响应流。
#1
1
This will call a second controller action from the first controller action within the same controller:
这将从同一控制器中的第一个控制器操作调用第二个控制器操作:
public ActionResult FirstAction()
{
// Do FirstAction stuff here.
return this.SecondAction(ArgumentsIfAny);
}
public ActionResult SecondAction()
{
// Do SecondAction stuff here.
return View();
}
Doesn't need to be too complicated. :-)
不需要太复杂。 :-)
#2
0
The idea of most MVC frameworks it to make things more simple. Everything breaks down into a call to a method with certain inputs and with certain return values. In a way, you can accomplish what you want by doing something like this:
大多数MVC框架的想法让它变得更简单。一切都分解为对具有某些输入和某些返回值的方法的调用。在某种程度上,您可以通过执行以下操作来完成您想要的任务:
class MyController {
public ActionResult Action1() {
// Do stuff 1
}
public ActionResult Action2() {
// Do stuff 2
}
}
You can then refactor a bit:
然后你可以重构一下:
class MyController {
public ActionResult Action1() {
// Pull stuff out of ViewData
DoStuff1(param1, param2, ...);
}
public ActionResult Action2() {
DoStuff2(param1, param2, ...);
}
public void DoStuff1(/* parameters */) {
// Do stuff 1
}
public void DoStuff2(/* parameters */) {
// Do stuff 2
}
}
Now you can just call DoStuff1() and DoStuff2() directly because they're just methods. You can make them static if possible. Don't forget that you'll likely need to do something about error checking and return types.
现在你可以直接调用DoStuff1()和DoStuff2(),因为它们只是方法。如果可能,您可以将它们设为静态。不要忘记您可能需要对错误检查和返回类型执行某些操作。
#3
0
protected override void OnActionExecuted(ActionExecutedContext filterContext)
{
base.OnActionExecuted(filterContext);
this.ViewData.Model = GLB_MODEL;
Stream filter = null;
ViewPage viewPage = new ViewPage();
viewPage.ViewContext = new ViewContext(filterContext.Controller.ControllerContext, new WebFormView("~/Views/Customer/EmailView.aspx", ""), this.ViewData, this.TempData);
var response = viewPage.ViewContext.HttpContext.Response;
response.Clear();
var oldFilter = response.Filter;
try
{
filter = new MemoryStream();
response.Filter = filter;
viewPage.ViewContext.View.Render(viewPage.ViewContext, viewPage.ViewContext.HttpContext.Response.Output);
response.Flush();
filter.Position = 0;
var reader = new StreamReader(filter, response.ContentEncoding);
string html = reader.ReadToEnd();
}
finally
{
if (filter != null)
{
filter.Dispose();
}
response.Filter = oldFilter;
}
}
This is modified version of code from Render a view as a string. I didn't want to render the result of the view to the httpcontext response stream.
这是将视图渲染为字符串的代码的修改版本。我不想将视图的结果呈现给httpcontext响应流。