not found exception is not working in web api [closed] - c#

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
my code
public async Task<IHttpActionResult> Post(long? Workrequestid, string fileexist){ throw new HttpResponseException(Request.CreateErrorResponse(HttpStatusCode.NotFound,"this item does not exist"));}
error i am getting like
{ "message": "Processing of the HTTP request resulted in an exception. Please see the HTTP response returned by the 'Response' property of this exception for details."
}
according to me i need the message like "this item already exist". please help me

Your implementation does work as expected. Are you calling the API correctly? I've used TestController and posting to the following Uri and it works:
http://localhost:8075/api/test?workrequestId=1&fileexist=test
However, are you sure you want to explicitly throw an exception? When returning an IHttpActionResult it would be cleaner to simply return a 404 in the following way:
return this.NotFound();
Returning a NotFound error is telling the calling client all it needs to know, returning "this item does not exist" is redundant.
As there is no implementation on your code sample it's difficult to see your intention for the API other that it's a post. Depending on the circumstances, if appropriate, you could also return:
return this.BadRequest("this item does not exist.");

Related

C# Syntax for discard variable and syntax for chaining [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 months ago.
This post was edited and submitted for review 7 months ago and failed to reopen the post:
Original close reason(s) were not resolved
Improve this question
I have came across the following code:
EnsureGraphForAppOnlyAuth();
_ = _appClient ??
throw new System.NullReferenceException("Graph has not been initialized for app-only auth");
I understand if _appClient is null, it will throw an exception. (1)My question is what is _ =? (2)What's it called so I can look it up in a reference?
Next, in continuation of the code, I have this
return _appClient.Groups
.Request()
.Select(u => new
{
u.DisplayName,
u.Id
})
// Get at most 25 results
.Top(25)
// Sort by display name
.OrderBy("DisplayName")
.GetAsync();
(3)Is this chaining?
.Groups
.Request()
.Select()
.OrderBy()
.GetAsync()
Doesn't each function depend on the return value of the prior function? If so, there's no error checking or checking the return value prior to calling the next function? What happen if these where all async?
Also, if I want to write a class and its function so a client can do the same thing, (4) do I have to do anything special when designing the class except of each function return a value?
_ is a valid variable name in C#. Usually, developers use it as a placeholder to denote that 'this is required for the syntax to be valid, but I don't intend to use it'
Apparently it is called a discard. I didn't know that it had a real name.
Yes, that's using a fluent API style to chain methods together. Each method uses the return value of the previous method as its parameter.
Take a look at how the LINQ library is set up. You'll want to use extension methods similarly

Is it good idea to have a general try/catch in every method in a controller or is there a better way? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 5 years ago.
Improve this question
I'm trying to improve my MVC.NET Core-fu and I ended up in a bunch of methods in all my controllers looking like this (note the outermost, general, repetitive try-catch).
[HttpPost]
public IActionResult DoSomething([FromBody] Thing thing)
{
try
{
if (...)
return Ok(thing);
else
return NotFound();
}
catch (Exception) { return BadRequest(); }
}
The way I see, I want to have the baddy-requesty just in case. Now, that adds a lot of code to otherwise rather simple controller and I have a suspicion that someone before has thought of this and proposed a solution.
I haven't found such thing (and I was informed that filters are obsolete for usage in Core). Possibly it's because I'm not familiar with the right search keys.
This seems excessive to me. Many methods won't execute instructions that are likely to fail. For those extremely rare cases, a global error handler would be more than sufficient.
For those methods that access the disk, a database, or perform calculations that could raise exceptions (something you should probably be avoiding in the first place), a try...catch block makes sense.
But even in these cases, if a method with a try...catch handler calls another method, there is no reason to put handlers in that other method.

Need help to interpret this. I am a beginner in C# [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I been searching for ages about IEnumerable online but they are all in the program.cs file.
Can anyone please tell me what is it at the below code?
namespace customBank.Interfaces
{
public class Bank : customBank // the bank take the format of customBank as interface.
{
public IEnumerable<IStatementRow> GetMiniStatement(IAccount account)
}
}
IEnumerable is a list of "things" that you can loop through. In this situation, it is a list of IStatementRow things.
A list of IStatementRow things is returned when the GetMiniStatement function is called.
See: IEnumerable Interface
Also see: Interfaces
Once you read that you will understand pretty well whats happening on your code.

Unity3d "Cannot implicitly convert type `void' to `bool'" [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
void Update ()
{
if (transform.Translate((Input.acceleration.x), 0, 0))
{
GetComponent<Rigidbody>().AddForce(Vector2.right);
}
}
I am using the accelerator for a Android App and doesn't seem to like it. I can change it to work for KeyDown but it wont work with the accelerator.
Error:
"Cannot implicitly convert type "void" to "bool"
According to the documentation http://docs.unity3d.com/ScriptReference/Transform.Translate.html:
transform.Translate does not return a boolean; its return type is void. Therefore you cannot use an if statement to evaluate whether or not it was successful.
If you want to check to see if the translate happened correctly you would need to check the side effects from calling transform.Translate. In other words see what has changed on the transform and see if it matches your expectations.
Don't use the if statement, as Translate returns nothing. Just leave the AddForce line

can we Throwing Exception to a specific method c#? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I've cascading drop downs in my c# winform application and i'm getting data from wcf service and filling the drop downs. I want if any exception occurs in my event handlers then i should be able to throw exception to a specified method.
Is that Possible? Any syntax for this?
something like this
MethodName(throw);
No, you can't throw an exception to a specific method. The exception always bubbles up the call stack.
You can handle an exception by calling another method. In that case, just pass the exception to it:
try
{
}
catch (Exception ex)
{
ShowErrorToUser(ex);
}
private void ShowErrorToUser(Exception ex)
{
MessageBox.Show(ex.Message);
}
There is no built-in way to do this. Exceptions are raised and (simplified) bubbling up the call stack. So it always has to be a method in the call hierarchy that catches the exception.
You will need to add a call to the method that should handle the exception in your catch blocks.

Categories

Resources