从外部站点发布到已键入参数的 MVC 控制器
我是 MVC 新手,所以我一直在努力解决问题的标题——不确定是否有任何内容已经涵盖了我想做的事情。
假设我有一个控制器:FooController。它处理一组视图,所有视图都执行相同的基本操作,但由于各种原因使用不同的模型。因此,我为每个模型创建一个操作,这效果很好:
[HttpPost]
public ActionResult Bar(Type1 t);
[HttpPost]
public ActionResult Bar(Type2 t);
等等...
我想要完成的(如果可能的话)是创建一个可以接受来自第 3 方网站的 POST 的操作(除了类型化视图之外)本地),我似乎无法弄清楚如何在不 MVC 抛出异常的情况下接受数据:
当前对控制器类型“FooController”上的操作“Bar”的请求在以下操作方法之间不明确:
并且它列出了上述操作。
我尝试了几件事:
- 创建了一个方法 Bar(ExternalSiteType t) - 异常
- 创建了一个方法 Bar(ExternalSiteParam1 p,ExternalSiteParam2 p2) - 异常
- 注释掉了其他操作 - 上述两种方法都成功了
任何想法如何让它工作?
编辑:路线示例
/Foo/Bar/Type1
/Foo/Bar/Type2
/Foo/Bar/ExternalSite
I'm new to MVC so I was struggling with the title of the question -- not sure if there's anything already covering what I want to do.
Suppose I have a controller: FooController. It handles a set of views, all of which do the same basic thing, but use different models for various reasons. So I create an action for each model, and this works well:
[HttpPost]
public ActionResult Bar(Type1 t);
[HttpPost]
public ActionResult Bar(Type2 t);
etc...
What I want to accomplish (if possible) is to create an action that can accept a POST from a 3rd party website (in addition to the typed-views locally) and I can't seem to figure out how to accept the data without MVC throwing an exception:
The current request for action 'Bar' on controller type 'FooController' is ambiguous between the following action methods:
And it lists the above actions.
I've tried a couple things:
- Created a method Bar(ExternalSiteType t) - Exception
- Created a method Bar(ExternalSiteParam1 p, ExternalSiteParam2 p2) - Exception
- Commented out the other actions - Success for both methods above
Any ideas how to get this to work?
EDIT: Route examples
/Foo/Bar/Type1
/Foo/Bar/Type2
/Foo/Bar/ExternalSite
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为
根本无法工作,即使它来自同一个站点。不能有两个具有相同名称的操作,除非它们使用不同的 Http 动词(
[HttpGet]、[HttpPost]
等)。最好的方法是为外部站点创建一个具有不同名称的操作,将传入的信息转换为
Type1
,然后重定向到Bar(Type1 t)
。有关重定向和保留的不同方式,请参阅ASP.NET MVC Redirect with model你的复杂对象。I don't think
could work at all, even if it was from the same site. You can't have two actions with the same name unless they use different Http verbs (
[HttpGet],[HttpPost]
etc).Your best best would be to create an action with a different name for the external site, convert the passed in information to
Type1
and then Redirect toBar(Type1 t)
. See ASP.NET MVC Redirect with model for different ways you could Redirect and preserve your complex object.