开发者

Add a .asmx file to asp.net MVC?

开发者 https://www.devze.com 2022-12-12 03:54 出处:网络
Is it possible to add a .asmx file to an MVC project and hav开发者_开发百科e .asmx code call the controller and have the controller return data to the .asmx code?

Is it possible to add a .asmx file to an MVC project and hav开发者_开发百科e .asmx code call the controller and have the controller return data to the .asmx code?


Is it possible to add a .asmx file to an MVC project

Yes.

asmx code call the controller and have the controller return data to the .asmx code?

Yes.

var controller = new YourController();
var httpContext = new HttpContextWrapper(this.Context);
var routeData = new RouteData();
var requestContext = new RequestContext(httpContext, routeData);
controller.ControllerContext = new ControllerContext(requestContext, controller);

var result = controller.YourAction();

But I discourage to do that. Instead move common logic into a separate service layer and use that one.


Unless you need functionality specific to the controller (such as rendered output, web context, etc), you could take out the logic you want to execute in the controller and put it in a new class and have both controller and webservice call the method in the new class.


You can call your asmx by adding an Ignore Route path in your global.asax RegisterRoute method


I can't answer the first part (though I assume it's possible), but the second part is simple.

It can of course access the controller because the controller is just a class with functions that return results. It will simply be a matter of setting the controller's HttpContext correctly, and then invoking the action method that you want. You can then do whatever you want with the ActionResult - render it to a string for instance.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号