-
Dotnet 6 API with a HttpDelete and that 405 Method not allowed error
As we can tell from the title of the Post, we are going to talk about figuring out why in a client app one gets a Http Status code of 405 (Method Not Allowed).
When does this happen? Usually when a http request does a http PUT or DELETE. Yet, when one uses Swagger (openAPI) or Postman to directly call the same API Delete or Put, it works! This is starting to feel like a CORS issue, right? Read On ==>
So 405! Indeed looking at the surface of what is going on, one thinks Ah! CORS is at it again! Absolutely, this is what it feels like. After all, a Swagger/Postman GET/DELETE call under the same http domain works. But the moment one moves to a Client App coming in from a different domain (i.e. a different port on localhost, because different ports are also considered a different domain in terms of CORS), the Http PUT/DELETE stops working and throws a 405.
We are using VS2022 with Dotnet 6.0.100-rc.2.21505.57 (so the latest VS2022 and Dotnet RC 6 as of the writing of this post)
Wow… let’s step into how to rein in CORS…. We start in the API project in the Startup class.
In Configure Services =>
services.AddCors(options => { options.AddPolicy("CorsPolicy", builder => builder .AllowAnyOrigin() .AllowAnyHeader() .AllowAnyMethod() .WithMethods("GET","PUT","DELETE","POST","PATCH") //not really necessary when AllowAnyMethods is used. ); });
We build a CORS policy (yes one can remove the .WithMethods clause)
And of course we add in the Configure method =>
app.UseRouting(); app.UseCors("CorsPolicy");
And everything is right with the world… Let’s do a Delete in a client app. Meaning we are doing HttpClient Delete to a rest url that contains the id:
Yeap a hard 405… What the what What?
After digging through lots of readings on the topic of CORS 405 PUT/DELETE, DotNet 6 issues (with not much luck), I stepped upon the answer with a bit of thinking about the problem….In my Controller we have this as the delete method header =>
[HttpDelete("id")] public async Task<IActionResult> Delete(int id) { ...
Seems simple enough, it works in the Swagger interface for testing the API. I validated that the client is using and passing the correct url and id. WHAT!?
Look closely at the [HttpDelete… tag. Notice anything? If one said, “Duh, ScottGeek- you need to wrap the “id” within {}” then YEAP!
That would be the issue. The real question is why does this seem work in the Swagger UI for the api but not in a app using HttpClient?I have absolutely no idea why (yes I could get Fiddler out and start digging deep, nope) In this case what seems like a CORS acting up issue (405 on a cross domain request), yeah it’s not, or is it? This seems to be a bug somewhere, I suspect, in the OpenAPI (swagger) bits. Why in the OpenAPI? because it should reflect the same 405 when the API Method tag is not 100% correct.
But let’s not just pick on OpenAPI. The fact that DotNet 6 allows both [HttpDelete(“id”)] and [HttpDelete(“{id}”)] without issuing some kind of build blowup or at least a green squiggle on the one that is in error… well I give it to ya… feels like a bug.
Put the {} around the id and….
Also, don’t forget to check one’s HttpPut as well…
~SG
-
Autumn 2021
….
Welcome Autumn!
….
-
Blazor with dotnet watch run in the Pre-Dotnet 6 and VS 2022 days
Oh, how we do wait for the release of dotnet 6 and most important VS 2022. Why? Well the promise of hot-reload without hoops to jump through.
For the most part in VS 2019 16.10.4, “hot-reload” of a Blazor app is not too bad <= Circle that “not too bad”- it does actually work… mostly! Yes, I do refer to the “dotnet watch run”. It does detect changes in your Blazor files and reload the browser page… yeah, BUT it does have issues. Here comes the list….
It doesn’t detect new files added… go ahead add a new razor page while your project is in “dotnet watch run” – nope it will not find it no matter how many times you hard refresh the browser. Not only razor pages, but new stylesheets- yeap, it’s not going to find those either!
It has issues when you make too many complicated changes in your code. Yes compile errors that don’t go away.
It interferes with an already “flaky” intellicode/intellisense… My goodness talking about annoying! Already half the time, bootstrap intellisense stops working on razor pages. And what the hell happened to code snippets? (Probably less about hot-reload than this version of VS being riddled with bugs!)
Ok, I’m mostly over that now…. deep breath… 1…2…3…4… Alright.
Let’s get to the point, the matter of always having to type in that “dotnet watch run”, stop it, and type it in again… what’s the point? Let’s create a launch profile so that we can tack this into VS (gee you think the Blazor Template should already have that? Yeap it should). Here we go:
"Watch": { "commandName": "Executable", "launchBrowser": true, "launchUrl": "http://localhost:5000/", "commandLineArgs": "watch run", "workingDirectory": "$(ProjectDir)", "executablePath": "dotnet.exe", "environmentVariables": { "ASPNETCORE_ENVIRONMENT": "Development" } }
In your launchSettings.Json file, place the code above in the “Profile” section…. Match sure this is in the “PROFILE” section!
Now let the magic begin… well mostly… When you tap the “Watch” on the Build/Debug drop down… that lovely “dotnet watch run” will execute in a window… and now you to can experience the “buggy” hot-reload of a Blazor app. 😎
~SG -
BCP and Clustered SQL Oh the Errors
I got a curious message concerning problems that someone from work was having issues getting a relatively simple BCP (Bulk Copy for SQL) to work.
So here we go:
bcp "select top 5 [Data] from [ADataBase].[dbo].[ATable] where [ProcessedTime] <= GetDate()-1 and MessageType=''SomeProduct'' order by [ProcessedTime] desc for xml raw, root(''MyexportRoot'')"' + ' QUERYOUT ""B:\BCPBatch\Data\sample.xml"" -T -c -S TheSQL\Instance';
It’s a little complicated… Basically pull data from a table in DB on a SQL Instance… and output as xml to an xml file.
After spending, a few hours, getting all kinds of errors about drive not found, path not found, can’t open the file… bah bah bah. It’s like what the hell?!You take that statement and execute it on a console window or powershell window… and no problems, but you get into SSMS, and in a query window… and nothing but errors… So something like this:
EXECUTE sp_configure 'show advanced options', 1; GO -- To update the currently configured value for advanced options. RECONFIGURE; GO -- To enable the feature. EXECUTE sp_configure 'xp_cmdshell', 1; GO -- To update the currently configured value for this feature. RECONFIGURE; GO declare @cmd nvarchar(512); -- Please note that the fully qualified table name is needed select @cmd = 'bcp "select top 5 [Data] from [ADataBase].[dbo].[ATable] where [ProcessedTime] <= GetDate()-1 and MessageType=''SomeProduct'' order by [ProcessedTime] desc for xml raw, root(''MyexportRoot'')"' + ' QUERYOUT ""B:\BCPBatch\Data\sample.xml"" -T -c -S TheSQL\Instance'; exec xp_cmdshell @cmd; go EXECUTE sp_configure 'xp_cmdshell', 0; GO -- To update the currently configured value for this feature. RECONFIGURE; GO EXECUTE sp_configure 'show advanced options', 0; GO
And let the errors happen… and they do. What’s the issue here… well nothing really with the code, but with the fact that my TheSql\Instance was in fact Clustered!
So why is that a problem? It’s this part here:
QUERYOUT “”B:\BCPBatch\Data\sample.xml”” – the syntax is correct, but on a clustered server file locations are problematic! Meaning, the cluster does not normally have full access to all of the drives. Now, will a network share work? Not sure. I didn’t test that.
The solution I suggested was to move to another SQL instance that was not clustered, and the script worked.
~SG -
Micro Build 2021 Is Here!
YES Build is here! I’m really liking the Backpack!
~ScottGeek
-
A slight issue when one wants to install SQL 2019 Dev, etc The ODBC 17 SQL Driver
On my way to getting the latest SQL Server installed on one of my Dev PC’s- I ran across the issue of getting prompt for:
msoledbsql.msi
What the hell…
ODBC Driver 17 is what that is about. Apparently, the SQL installed is not smart enough to know that one already has ODBC 17 SQL Driver installed and runs into a major “cannot resolve” the problem. The end result is that one does not get the SQL instance installed…
But after reading lots of “not really helping” blurbs from the Internet, the Solution is simple… well mostly.
You have to uninstall the “Microsoft ODBC Driver 17 for SQL Server” – one can find it on windows 10 in the APPS & Features. Uninstall it and redo the SQL install. One should have better luck with moving forward with getting SQL installed…
~ScottGeek
-
More Blazor talk from the Community Stand up
Links from the show.
[visual-link-preview encoded=”eyJ0eXBlIjoiZXh0ZXJuYWwiLCJwb3N0IjowLCJwb3N0X2xhYmVsIjoiIiwidXJsIjoiaHR0cHM6Ly93d3cudGhldXJsaXN0LmNvbS9tYXJjaC05LWNvbW11bml0eS1saW5rcyIsImltYWdlX2lkIjotMSwiaW1hZ2VfdXJsIjoiaHR0cHM6Ly93d3cudGhldXJsaXN0LmNvbS9pbWFnZXMvbG9nby5wbmciLCJ0aXRsZSI6IlRoZSBVcmxpc3QgLSBDb250cmlidXRlIHRvIEJsYXpvciBMaW5rcyIsInN1bW1hcnkiOiJBU1AuTkVUIENvbW11bml0eSBTdGFuZHVwIC0gSG93IHRvIGNvbnRyaWJ1dGUgdG8gQmxhem9yIiwidGVtcGxhdGUiOiJ1c2VfZGVmYXVsdF9mcm9tX3NldHRpbmdzIn0=”]
-
Community links for .net Network API community standup
[visual-link-preview encoded=”eyJ0eXBlIjoiZXh0ZXJuYWwiLCJwb3N0IjowLCJwb3N0X2xhYmVsIjoiIiwidXJsIjoiaHR0cHM6Ly93d3cudGhldXJsaXN0LmNvbS9hc3BuZXQtc3RhbmR1cC0yMDIxLTAyLTE2IiwiaW1hZ2VfaWQiOi0xLCJpbWFnZV91cmwiOiJodHRwczovL3d3dy50aGV1cmxpc3QuY29tL2ltYWdlcy9sb2dvLnBuZyIsInRpdGxlIjoiVGhlIFVybGlzdCAtIC5uZXQgbmV0d29yayBBUEkiLCJzdW1tYXJ5IjoiTGlua3MgZnJvbSBDb21tdW5pdHkgU3RhbmR1cCAyMDIxLTAyLTE2IiwidGVtcGxhdGUiOiJ1c2VfZGVmYXVsdF9mcm9tX3NldHRpbmdzIn0=”]
And
[visual-link-preview encoded=”eyJ0eXBlIjoiZXh0ZXJuYWwiLCJwb3N0IjowLCJwb3N0X2xhYmVsIjoiIiwidXJsIjoiaHR0cHM6Ly93d3cudGhldXJsaXN0LmNvbS9hc3BuZXQtc3RhbmR1cC0yMDIxLTAyLTIzIiwiaW1hZ2VfaWQiOi0xLCJpbWFnZV91cmwiOiJodHRwczovL3d3dy50aGV1cmxpc3QuY29tL2ltYWdlcy9sb2dvLnBuZyIsInRpdGxlIjoiVGhlIFVybGlzdCAtIFNoYXJlIHRoZSBpbnRlcm5ldCIsInN1bW1hcnkiOiJHcm91cCBsaW5rcyB0b2dldGhlciBpbiBhIGxpc3QgYW5kIGdldCBhIGN1c3RvbSBVUkwgdG8gc2hhcmUgdGhlbS4gRWRpdCB5b3VyIGxpc3RzIGF0IGFueSB0aW1lLiIsInRlbXBsYXRlIjoidXNlX2RlZmF1bHRfZnJvbV9zZXR0aW5ncyJ9″]
-
Community links for the February 9th edition of the Blazor community standup
[visual-link-preview encoded=”eyJ0eXBlIjoiZXh0ZXJuYWwiLCJwb3N0IjowLCJwb3N0X2xhYmVsIjoiIiwidXJsIjoiaHR0cHM6Ly93d3cudGhldXJsaXN0LmNvbS9mZWItOS1jb21tdW5pdHktbGlua3MiLCJpbWFnZV9pZCI6LTEsImltYWdlX3VybCI6Imh0dHBzOi8vd3d3LnRoZXVybGlzdC5jb20vaW1hZ2VzL2xvZ28ucG5nIiwidGl0bGUiOiJUaGUgVXJsaXN0IC0gQmxhem9yIExpbmtzIiwic3VtbWFyeSI6Ikxpbmsgc2l0ZXMgZm9yIEJsYXpvciIsInRlbXBsYXRlIjoic2ltcGxlIn0=”]
-
The GitHub Secret thing
So you travel along in your or someone’s GitHub repo… and we all know what that looks like. The repo code, actions, etc. But did you know that you can make the GitHub Web UI (page) look like a VS Code File Explorer? Ya… you can. It’s not really supported by GitHub (just to be clear)… but yeah.
What is magic…
Normally a GitHub url looks something like https://github.com/<User Name>/<Repo Name> simple enough…
To see the magic… just do a https://github1s.com/<User Name>/<Repo Name>
That’s right, add a 1s to the DNS name of github.com… You will get a VS Code “On-Line” view of the repo. How cool is that? 😎Look in the GH repo: /github1s
~SG