ASP.NET MVC 中按发布内容类型进行路由

发布于 2024-11-03 03:39:01 字数 298 浏览 2 评论 0原文

我有一个固定的 URL,我想向其中发布不同类型的 xml 消息,并使用 DataContracts 进行反序列化。根据反序列化消息的类型,我想路由到: 重载方法,例如

void Process(ContractType1 request) {}
void Process(ContractType2 request) {}

,因此在某些时候我需要反序列化此消息并希望允许默认路由规则匹配正确的方法。我应该使用哪个扩展点?或者更好的是,我可以开箱即用吗?

如果有什么区别的话,我正在使用 MVC 3。

I have a fixedURL to which I'd like to post different types of xml message, deserialized using DataContracts. Depending on the type of the deserialized message, I'd like to route to:
overloaded methods, e.g.

void Process(ContractType1 request) {}
void Process(ContractType2 request) {}

So at some point I need to deserialize this message and hopefully allow the default routing rules to match the correct method. Which extensibility point should I use for this? Or even better, can I make this work out of the box?!

If it makes any difference, I'm using MVC 3.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

后来的我们 2024-11-10 03:39:01

如果它们没有针对不同的 HTTP 方法进行修饰,则 ASP NET MVC 不会考虑重载 - 例如,一个用于 POST,另一个用于 GET

您需要使用[ActionName(Name = "Process2")]来更改路由名称。并且你将不得不使用不同的路由来访问(如果HTTP方法相同)

看看此处


除了技术解决方法之外,将不同的合约传递到同一 URL 违反了REST原则。数据可以采用不同的格式(XML、JSON...),但必须相同。 URI 定义了一个独特的意图。现在可以有一个通用的转储程序,其中文档都转储到同一个 URI,但 ASP NET MVC 默认模型绑定器将无法帮助您,您需要创建您自己的模型绑定器

ASP NET MVC does not respect the overload if they are not decorated for different HTTP methods - e.g. one for POST, other for GET.

You need to use [ActionName(Name = "Process2")] to change the route name. And you will have to use different routes to access (if the HTTP methods are the same)

Have a look here.


Apart from the technical workaround, passing different contracts to the same URL is against the REST principles. Data could be in different format (XML, JSON, ...) but it must be the same. The URI defines a unique intention. Now it is possible to have a common dumpster where documents are all dumped to the same URI but then ASP NET MVC default model binder would not be able to help you and you need to create your own model binder.

故事与诗 2024-11-10 03:39:01

与其他答案相反,我说这是可能的

Asp.net MVC 是一个可以轻松扩展的出色平台。因此,基本上我编写了一个特殊的操作方法选择器,它使得编写可以服务相同 HTTP 方法但延迟参数的重载成为可能。默认情况下,您会收到操作方法无法解析的运行时错误。但是当您使用此操作方法选择器时,您就可以摆脱此错误。

基本上,如果您的参数类具有不同的参数名称,您实际上可以通过它来选择方法。

操作方法选择器称为 RequiresRouteValuesAttribute ,典型的使用场景是使用默认路由,其中​​ id 是可选的:

{controller}/{action}/{id}

这意味着您要么必须编写

public ActionResult Index(int? id)
{
    if (id.HasValue)
    {
        // display details view
    }
    else
    {
        // display master view
    }
}

,要么使用我的操作方法选择器,您可以轻松编写两个操作方法:

public ActionResult Index()
{
    // display master view
}

[RequiresRouteValues("id")]
public ActionResult Index(int id)
{
    // display details view
}

只要您的自定义类型具有不同的属性名称或方法使用不同的参数名称,那么同样可以应用于您的操作方法。因此,在您的情况下,它可能类似于:

[RequiresRouteValues("first.Id")] // when you provide prefix with your form
// or
[RequiresRouteValues("Some.ContractType1.Distict.Property.Name")]
public ActionResult Process(ContractType1 first)
{
    // do what's appropriate
}

[RequiresRouteValues("second.Id")] // when you provide prefix with your form
// or
[RequiresRouteValues("Some.ContractType2.Distict.Property.Name")]
public ActionResult Process(ContractType2 second)
{
    // do what's appropriate
}

阅读所有详细信息< /a> 关于此操作方法选择器并获取代码。

Contrary to the other answer I say this is possible

Asp.net MVC is a great platform that can be easily extended. And so basically I've written a special action method selector that makes it possible to write overloads that can serve the same HTTP method but defer in parameters. By default you'd get runtime error that action method can't be resolved. But when you use this action method selector you get rid of this error.

Basically if your parameter classes have distinct parameter names, you can actually select methods by that.

Action method selector is called RequiresRouteValuesAttribute and a typical usage scenario would be with default route where id is optional in:

{controller}/{action}/{id}

This means that you either have to write

public ActionResult Index(int? id)
{
    if (id.HasValue)
    {
        // display details view
    }
    else
    {
        // display master view
    }
}

but by using my action method selector you can easily write two action methods:

public ActionResult Index()
{
    // display master view
}

[RequiresRouteValues("id")]
public ActionResult Index(int id)
{
    // display details view
}

The same could be applied to your action methods as long as your custom types have distinct property names or methods use different parameter names. So in your case it could be something like:

[RequiresRouteValues("first.Id")] // when you provide prefix with your form
// or
[RequiresRouteValues("Some.ContractType1.Distict.Property.Name")]
public ActionResult Process(ContractType1 first)
{
    // do what's appropriate
}

[RequiresRouteValues("second.Id")] // when you provide prefix with your form
// or
[RequiresRouteValues("Some.ContractType2.Distict.Property.Name")]
public ActionResult Process(ContractType2 second)
{
    // do what's appropriate
}

Read all the details about this action method selector and get the code as well.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文