Readdy Write  
0,00 €
Your View Money
Views: Count
Self 20% 0
Your Content 60% 0

Users by Links 0
u1*(Content+Views) 10% 0
Follow-Follower 0
s2*(Income) 5% 0

Count
Followers 0
Login Register as User

Asp.Net Core API : Response Statustext ist nicht mehr vorhanden

26.02.2021 (👁2532)


 

Leider ist in Asp.Net Core beim Wechsel von MVC auf API bei den Antworten eines Controllers im Fehlerfall kein response.statusText mehr vorhanden.

Scheinbar wurde dieser deaktiviert.

In Asp Core API .Net5 kann auch in der Startup.cs bei Services die Unterdrückung nicht ausgeschaltet werden.

 

 

Asp.Net Core Web API
Status Code – 123 (Response:StatusCode)

return StatusCode(401,"You are not the Owner");

 

Response in React Client

Leider ohne Statustext

status 401

statusText    ''

 

 

Name

Value

Type

response

Response {type: 'basic', url: 'https://localhost:44315/api/articles/33', redirected: false, status: 401, ok: false, …}

object

get body

ƒ body()

function

bodyUsed

false

boolean

headers

Headers

object

ok

false

boolean

redirected

false

boolean

status

401

number

statusText

''

string

type

'basic'

string

url

'https://localhost:44315/api/articles/33'

string

 

 

 

Status Code – 400 (Bad Request)

 

return BadRequest("Statustext of Bad Request 400");

Leider auch ohne StatusText im React Client

Name

Value

Type

response

Response {type: 'basic', url: 'https://localhost:44315/api/articles/0', redirected: false, status: 200, ok: true, …}

object

get body

ƒ body()

function

bodyUsed

false

boolean

headers

Headers

object

ok

true

boolean

redirected

false

boolean

status

200

number

statusText

''

string

type

'basic'

string

url

'https://localhost:44315/api/articles/0'

string

 

Folgende Code Versionen habe ich ausprobiert

if (IDOwner != sIDUser) {

return BadRequest("everything wrong");

//StatusCode(StatusCodes.Status403Forbidden,"nothing in asp");

//StatusCode(StatusCodes.Status403Forbidden, new ApiError() { ErrorMessage = "this is in the statusText" });

//StatusCode(StatusCodes.Status406NotAcceptable,"User is not the Owner");

//return BadRequest(new ApiError(){ErrorMessage = "this is in the statusText" });

//JsonResult myResponse = new JsonResult("StatusCode");

//return myResponse ;

//return BadRequest(sResult);

///return new BadRequestObjectResult("StatusText for BadRequest");

//return BadRequest("StatusText for BadRequest");

}

//</ check Owner >

}

 

Laut einer Anleitung aus MVC kann man die Unterdrückung in der Startup.cs deaktivieren, aber das hat auch zu keinem Erfolg geführt

            services.AddMvc()

            .ConfigureApiBehaviorOptions(options =>

            {

                options.SuppressMapClientErrors = false;    // <-- THIS SHOULD BE FALSE

                //options.InvalidModelStateResponseFactory = context =>

                //{

                //    var result = new BadRequestObjectResult(context.ModelState);

                //    // TODO: add `using System.Net.Mime;` to resolve MediaTypeNames

                //    result.ContentTypes.Add(MediaTypeNames.Application.Json);

                //    result.ContentTypes.Add(MediaTypeNames.Application.Xml);

                //    return result;

                //};

            });

 

 

Return StatusCodes in Asp.Net Core API

 

Angezeigter Status Code in React bei verwendung von return StatusCode(..) in Asp.Net Core API

 

 

Und kann dann entsprechend des Response.Status Codes im Client reagieren

 

        };

        const response = await fetch('api/articles/' + this.state.idarticle, requestOptions); //*SEND DATA

        if (response.status !== 0) {

            if (response.status === 400) alert("Bad Data Request");

            else if (response.status === 403) alert("User is not Owner");

            else alert('send error:' + response.statusText);

        }

 

 

 

Return Values for StatusCodes

Status

100

Continue

Status

412

PreconditionFailed

Status

413

PayloadTooLarge

Status

413

RequestEntityTooLarge

Status

414

RequestUriTooLong

Status

414

UriTooLong

Status

415

UnsupportedMediaType

Status

416

RangeNotSatisfiable

Status

416

RequestedRangeNotSatisfiable

Status

417

ExpectationFailed

Status

418

ImATeapot

Status

419

AuthenticationTimeout

Status

421

MisdirectedRequest

Status

422

UnprocessableEntity

Status

423

Locked

Status

424

FailedDependency

Status

426

UpgradeRequired

Status

428

PreconditionRequired

Status

429

TooManyRequests

Status

431

RequestHeaderFieldsTooLarge

Status

451

UnavailableForLegalReasons

Status

500

InternalServerError

Status

501

NotImplemented

Status

502

BadGateway

Status

503

ServiceUnavailable

Status

504

GatewayTimeout

Status

505

HttpVersionNotsupported

Status

506

VariantAlsoNegotiates

Status

507

InsufficientStorage

Status

508

LoopDetected

Status

411

LengthRequired

Status

510

NotExtended

Status

410

Gone

Status

408

RequestTimeout

Status

101

SwitchingProtocols

Status

102

Processing

Status

200

OK

Status

201

Created

Status

202

Accepted

Status

203

NonAuthoritative

Status

204

NoContent

Status

205

ResetContent

Status

206

PartialContent

Status

207

MultiStatus

Status

208

AlreadyReported

Status

226

IMUsed

Status

300

MultipleChoices

Status

301

MovedPermanently

Status

302

Found

Status

303

SeeOther

Status

304

NotModified

Status

305

UseProxy

Status

306

SwitchProxy

Status

307

TemporaryRedirect

Status

308

PermanentRedirect

Status

400

BadRequest

Status

401

Unauthorized

Status

402

PaymentRequired

Status

403

Forbidden

Status

404

NotFound

Status

405

MethodNotAllowed

Status

406

NotAcceptable

Status

407

ProxyAuthenticationRequired

Status

409

Conflict

Status

511

NetworkAuthenticationRequired