Webapi路由窘境 [英] Webapi routing quandary

查看:82
本文介绍了Webapi路由窘境的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的WebApi文件包含;

 public static void Register(HttpConfiguration config)
{
// Web API路由
config。 MapHttpAttributeRoutes();

config.Routes.MapHttpRoute(
name:DefaultApi,
routeTemplate:api / {controller} / {name},
defaults:new { name = RouteParameter.Optional}
);
}





我正在拨打ajax电话;

<脚本> 
$(document).ready(function(){
$(#grid)。bootgrid();

// onclick事件处理程序检索行信息
$(#grid)。on(click.rs.jquery.bootgrid,function(e,columns,row){
url ='api / AdminContentApi /'+ row.Name;

$ .ajax({
url:url,
类型:'GET',
成功:函数(d){
//在这里做我的东西!
},
错误:函数(xhr,textStatus,errorThrown){
alert(操作错误);
}
});
});
});
< / script>



,最后我的控制器代码;

 [授权(角色=管理员)] 
公共类AdminContentApiController:ApiController
{
[HttpGet]
public IHttpActionResult GetContentDetail(string name)
{
content c = ContentRepository.GetContentByName(name);
if(c == null)
{
return NotFound();
}
返回Ok(c);
}
}
}



我的问题和问题是,当调用被调用时,我希望网址为: api / AdminConTentApi / HOME_1

而不是我得到的; / AdminContent / api / AdminContentApi / HOME_L



哪里/如何/为什么/ WTF是在网址前面加上AdminContent吗?



谢谢



我的尝试:



根据我读过的所有内容我正在正确地做这件事,但已经在这个问题上工作了几个小时并且已经尝试了所有的内容。

解决方案

(文件).ready(function(){


(#grid)。bootgrid();

//用于检索行信息的onclick事件处理程序

(#grid)。on(click.rs.jquery.bootgrid,function(e,columns,row){
url ='api / AdminContentApi /'+ row .NAME;

My WebApi file contains;

public static void Register(HttpConfiguration config)
{
    // Web API routes
    config.MapHttpAttributeRoutes();

    config.Routes.MapHttpRoute(
        name: "DefaultApi",
        routeTemplate: "api/{controller}/{name}",
        defaults: new { name = RouteParameter.Optional }
    );
}



I'm making an ajax call;

<script>
    $(document).ready(function () {
        $("#grid").bootgrid();

        //onclick event handler to retrieve row info
        $("#grid").on("click.rs.jquery.bootgrid", function (e, columns, row) {
            url = 'api/AdminContentApi/' + row.Name;

            $.ajax({
                url: url,
                type: 'GET',
                success: function (d) {
                    //Do my stuff here!
                },
                error: function (xhr, textStatus, errorThrown) {
                    alert("Error in operation");
                }
            });
        });
    });
</script>


and finally my Controller code;

    [Authorize(Roles = "Admin")]
    public class AdminContentApiController : ApiController
    {
        [HttpGet]
        public IHttpActionResult GetContentDetail(string name)
        {
            content c = ContentRepository.GetContentByName(name);
            if (c == null)
            {
                return NotFound();
            }
            return Ok(c);
        }
    }
}


My question and the problem is that when the call is envoked I would expect the url to be: api/AdminConTentApi/HOME_1
instead I'm getting; /AdminContent/api/AdminContentApi/HOME_L

Where/How/Why/WTF is it prefixing the url with AdminContent?

Thanks

What I have tried:

According to all the tuts I've read I am doing it correctly but have been working for hours on this problem and have tried everything that tuts described.

解决方案

(document).ready(function () {


("#grid").bootgrid(); //onclick event handler to retrieve row info


("#grid").on("click.rs.jquery.bootgrid", function (e, columns, row) { url = 'api/AdminContentApi/' + row.Name;


这篇关于Webapi路由窘境的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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