ASP.Net的WebAPI:在BeginForm生成的URL无效 [英] ASP.Net WebApi: Invalid URL generated in BeginForm

查看:238
本文介绍了ASP.Net的WebAPI:在BeginForm生成的URL无效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开始使用的WebAPI和我有被错误地生成一个URL问题

I'm beginning to use WebApi and I'm having an issue with a URL being incorrectly generated

我有一个这样的ApiController:

I have an ApiController like this:

public class EntriesController : ApiController
{
    public HttpResponseMessage Post(Entry entry)
    {
    ...
    }
}

和我试图在同一个项目来测试这个API来创建一个标准控制器(即,不是的WebAPI)(我已经测试了小提琴手的API和一切正常那里)。

And I was trying to create a standard controller (i.e. not webapi) in the same project to test this api (I already tested the api with fiddler and everything is ok there).

我以为我可以使用标准的HTML助手是这样的:

I assumed I could use the standard HTML helpers like this:

@using (Html.BeginForm("Post", "Entries"))

然而,这会产生以下标记:

However this generates the following markup:

<form action="/Entries/Post" method="post">

和我预期产生

<form action="/api/Entries" method="post">

什么是生成从视图中的API网址的正确方法是什么?

What is the correct way to generate the API url from a view?

我使用的是默认的API和控制器的路线。

I am using the default api and controller routes.

感谢

推荐答案

您需要使用 BeginRouteForm 为纽带代的Web API的路线总是取决于航线名称。另外,还要确保提供所谓的路线值 httproute 如下图所示。

You would need to use BeginRouteForm as link generation to Web API routes always depends on the route name. Also make sure to supply the route value called httproute as below.

@using(Html.BeginRouteForm(DefaultApi,新{控制器=条目,httproute =真}))

这篇关于ASP.Net的WebAPI:在BeginForm生成的URL无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆