English 中文(简体)
HttpResponseException没有显示电文。
原标题:Messages are not displayed in HttpResponseException

我有一个例外类别,通过返回代码并写错误信息。 我看到该页上的回归法,但电文没有到达。 问题是什么?

public AppException(HttpStatusCode httpStatusCode, string message) 
        : base(message)
    {
        throw new HttpResponseException(new HttpResponseMessage(httpStatusCode)
        {
            Content = new StringContent(message)
        });
    }
}

放弃服务的例外

public async Task<User> CreateAsync(User user)
{
    if (EmailExists(user.Email))
    {
        var message = string.Format($"User with id={user.Email} already exists.");
        throw new AppException(HttpStatusCode.InternalServerError, message);
    }

    ....
}

我称之为控制器的服务方法:

/// <summary>
/// Creates a User
/// </summary>
/// <response code="201">User created</response>
/// <response code="400">Input data is invalid</response>
[HttpPost]
[ProducesResponseType(StatusCodes.Status201Created)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
public async Task<ActionResult<User>> CreateAsync(User user)
{
    if (!ModelState.IsValid)
    {
        return BadRequest(ModelState);
    }

    var result = await _userService.CreateAsync(user);
    return Ok(result);
}

在屏幕上,我执行了一项要求,即建立一个拥有现有电子邮件的用户。 它显示了回归法,但却没有显示信息。 “message” Field fin don t come food to the AppException category. Check:

“在此处的影像描述”/</a

问题回答

你们正试图放弃预期的<代码>。 HttpResponseException in the AppException Constructionor which is not the right or the recommended way to submitting excpetion. 合同制用于创建类别或初步确定物体。 NET产生基于这一信息的反应,并寄回客户,说明你为什么没有获得正确的错误信息。

To fix the issue you can choose one of the below way 1)Handle exception by using middleware 2)By using controller throw exception

在这方面,你可以尝试:

public class AppException : Exception
{
    public HttpStatusCode StatusCode { get; }

    public AppException(HttpStatusCode statusCode, string message) 
        : base(message)
    {
        StatusCode = statusCode;
    }
}

服务:

public async Task<User> CreateAsync(User user)
{
    if (EmailExists(user.Email))
    {
        var message = string.Format($"User with email={user.Email} already exists.");
        throw new AppException(HttpStatusCode.InternalServerError, message);
    }

    
}

controller:

[HttpPost]
[ProducesResponseType(StatusCodes.Status201Created)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
public async Task<ActionResult<User>> CreateAsync(User user)
{
    if (!ModelState.IsValid)
    {
        return BadRequest(ModelState);
    }

    try
    {
        var result = await _userService.CreateAsync(user);
        return Ok(result);
    }
    catch (AppException ex)
    {
        return StatusCode((int)ex.StatusCode, ex.Message);
    }
}




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签