错误:UrlHelper.Link不得返回null

时间:2018-12-30 21:42:47

标签: c# asp.net asp.net-mvc-5 web.api

我是c#.net的新手。我在这里创建REST服务,我想创建将服务数据插入数据库,但作为响应,我收到此错误 [HttpPost] [Route("api/CourseRegistrations")] [ResponseType(typeof(CourseRegistration))] public IHttpActionResult PostCourseRegistration(CourseRegistration courseRegistration) { if (!ModelState.IsValid) { return BadRequest(ModelState); } db.CourseRegistrations.Add(courseRegistration); db.SaveChanges(); return CreatedAtRoute("DefaultApi", new { id = courseRegistration.course_id }, courseRegistration); }

我的代码

Scanning dependencies of target zip
[  0%] Building C object lib/CMakeFiles/zip.dir/zip_add.c.obj
In file included from C:\Users\John\Desktop\C\libzip-1.5.1\lib\zip.h:61:0,
                 from C:\Users\John\Desktop\C\libzip-1.5.1\lib\zipint.h:49,
                 from C:\Users\John\Desktop\C\libzip-1.5.1\lib\zip_add.c:36:
c:\mingw\include\stdio.h:345:12: error: expected '=', ',', ';', 'asm' or '__attr
ibute__' before '__mingw__snprintf'
 extern int __mingw_stdio_redirect__(snprintf)(char*, size_t, const char*, ...);

            ^
lib\CMakeFiles\zip.dir\build.make:62: recipe for target 'lib/CMakeFiles/zip.dir/
zip_add.c.obj' failed
mingw32-make[2]: *** [lib/CMakeFiles/zip.dir/zip_add.c.obj] Error 1
CMakeFiles\Makefile2:154: recipe for target 'lib/CMakeFiles/zip.dir/all' failed
mingw32-make[1]: *** [lib/CMakeFiles/zip.dir/all] Error 2
Makefile:139: recipe for target 'all' failed
mingw32-make: *** [all] Error 2

1 个答案:

答案 0 :(得分:1)

我假设您正在WebApiConfig文件夹的App_Start类内使用默认设置,如下所示:

public static class WebApiConfig {
    public static void Register(HttpConfiguration config) {
        config.Routes.MapHttpRoute(
            name: "DefaultApi",
            routeTemplate: "api/{controller}/{id}",
            defaults: new { id = RouteParameter.Optional }
        );
    }
}

因此有必要在CreatedAtRoute方法中提供控制器名称参数,如下例所示:

return CreatedAtRoute("DefaultApi", new { controller = "ApiControllerName", id = courseRegistration.course_id }, courseRegistration);

但是,由于存在RouteAttribute,这表明已启用属性路由,因此您应该使用适当的参数名称为GET路由提供路由名称,然后设置POST路由,如下例所示:

// GET method
[HttpGet]
[Route("api/CourseRegistrations/{id}", Name = "GetCourseRegistrationById")]
[ResponseType(typeof(CourseRegistration))]
public IHttpActionResult GetCourseRegistration(int id)
{
    // do something
}

// POST method
[HttpPost]
[Route("api/CourseRegistrations")]
public IHttpActionResult PostCourseRegistration(CourseRegistration courseRegistration)
{
    if (!ModelState.IsValid)
    {
       return BadRequest(ModelState);
    }

    db.CourseRegistrations.Add(courseRegistration);
    db.SaveChanges();

    // replace 'DefaultApi' with route name for redirect to GET method
    return CreatedAtRoute("GetCourseRegistrationById", new { id = courseRegistration.course_id }, courseRegistration);
}

注意:

1)CreatedAtRoute中包含的参数名称必须与action方法中使用的目标参数名称匹配,否则UrlHelper.Link()方法将返回null。

2)RouteAttribute中的路由名称应与操作方法名称不同,以免造成混淆。

参考:

Attribute Routing in ASP.NET Web API 2: Route Names

相关问题:

Attribute Routing and CreatedAtRoute