.net core 6 api returns 404, but swagger works - asp.net-core-webapi

I have a .net 6 core api, everything works in my local environment. I can access swagger, and initiate a call throught he swagger ui, or through postman:
GET https://localhost:7194/List/ListXYZ/Item/270 HTTP/1.1
When I publish to my web server, I can acess the swagger UI, but I get a 404 when calling the api actions, either through the swagger ui or postman
GET https://myserver.mydomain.ca/Services/Myservice/List/ListXYZ/Item/270 HTTP/1.1
Returns:
HTTP/1.1 404 Not Found
Content-Type: text/html
Server: Microsoft-IIS/10.0
X-Powered-By: ASP.NET
Date: Mon, 24 Oct 2022 17:30:16 GMT
Connection: close
Content-Length: 1245
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
...
<div id="content">
<div class="content-container"><fieldset>
<h2>404 - File or directory not found.</h2>
<h3>The resource you are looking for might have been removed, had its name changed, or is temporarily unavailable.</h3>
</fieldset></div>
</div>
</body>
</html>
I can't find an error in the event log, and I can't seem to figure out if this is IIS configuration, incorrect logic in my api startup, etc. What should I be looking for here?
My controller looks like this:
[Route("[controller]")]
[ApiController]
public class ListController : ControllerBase
{
[HttpGetAttribute("{listName}/Item/{id}")]
public async Task<Item> GetItem(string listName, string id, [FromQuery]string[]? field = null)
{
}
}

Open program.cs,
move app.UseSwagger(); and app.UseSwaggerUI() outside the if condition, and you shall see the swagger running in your prod server.

Related

CSS won't load with UTF-8 encoding

trying for several days now to get my webapp to accept UTF-8 encoded requests. I'm out of ideas how to get that going.
Setup:
- DigitalOcean Droplet (Debian)
- Tomcat Container (8.5.43)
- JavaServlet Webapp
What i already did:
- set the locale at my Droplet to de_AT.UTF-8
- set the tomcat config to use UTF-8 for every request on 8080 via config.xml
...
<Connector port=8080 ... URIEncoding="UTF-8" ...>
...
- implemented a CharSetFilter, that is used for every request (web.xml holds the /* mapping for that filter). Code of the Filter:
#Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain next) throws IOException, ServletException {
request.setCharacterEncoding("UTF-8");
response.setContentType("text/html; charset=UTF-8");
response.setCharacterEncoding("UTF-8");
next.doFilter(request, response);
}
- set the encoding on every jsp file with
<%#page language="java" pageEncoding="UTF-8" contentType="text/html; charset=UTF-8"%>
- set the encoding on every html with
<meta charset="UTF-8" http-equiv="Content-Type" content="text/html; charset=UTF-8" />
- set the encoding in the css with
#charset "UTF-8";
use UTF-8 in eclipse as encoding before exporting to WAR
The CSS does not get loaded although it is referencable via the link in the browser-inspector.
I have deployed it so you can take a look at it:
http://2foliant.tk
Can anyone PLEASE help me out and tell me, why the CSS won't get loaded?
Remove the filter and see the result
Please remove the css tag since it is not related to css problem
Great to hear it is sorted
Accept this as a correct answer if you can do that

Changing MIME type with Meteor

When I'm running my app on localhost, I get 2 warnings concerning MIME type. This is one of them:
Resource interpreted as Stylesheet but transferred with MIME type text/html: "http://localhost:3000/BootstrapEssentials/bootstrap.css".
The other warning is identical with a different file. Both files are in my working directory. So far, I have been to these similar questions but they haven't helped:
Resource interpreted as stylesheet but transferred with MIME type text/html (seems not related with web server)
Chrome says "Resource interpreted as script but transferred with MIME type text/plain.", what gives?
Resource interpreted as stylesheet but transferred with MIME type text/html
Originally I was trying to use this line:
<link rel="stylesheet" href="/BootstrapEssentials/bootstrap.css">
I have since added in the type field:
<link rel="stylesheet" href="/BootstrapEssentials/bootstrap.css" type="text/css">
but that didn't do anything. I also have used the JavaScript Console to see that in the response header it has content-type: text/html; charset=utf-8 and I believe that if I can change that to being content-type: text/css; charset=utf-8 then everything will be fine but I can't find how to do that either.
As per the Meteor docs:
All files inside a top-level directory called public/ are served as-is to the client. When referencing these assets, do not include public/ in the URL, write the URL as if they were all in the top level. For example, reference public/bg.png as /bg.png. This is the best place for favicon.ico, robots.txt, and similar files.
Moving your stylesheets to a directory in public should do the trick!

Do I need to have a Startup.cs on my Frontend?

This question is related to this:
The problem I'm having is that I am trying to implement a SignalR with CORS from one ASP.NET (localhost:50000) project called Frontend to another ASP.NET MVC project (in the same solution) (localhost:60000).
When I run the application I get this error message in the console:
The following errors occurred while attempting to load the app.
- No assembly found containing an OwinStartupAttribute.
- No assembly found containing a Startup or [AssemblyName].Startup class. To disable OWIN startup discovery, add the appSetting
owin:AutomaticAppStartup with a value of "false" in your web.config.
To specify the OWIN startup Assembly, Class, or Method, add the
appSetting owin:AppStartup with the fully qualified startup class or
configuration method name in your web.config.
When I add a OWIN Startup class I don't get this error message:
HTTP Error 404.0 - Not Found The resource you are looking for has been
removed, had its name changed, or is temporarily unavailable.
And I guess it's because it is trying to find the hubs in it's own project.
Question 1:
Do I need a startup on my Frontend?
Question 2 (if Question 1 == yes):
Must I in the Startup map SignalR to my Backend?
EDIT:
index.html:
<!--Script references -->
<link href="Content/bootstrap.min.css" rel="stylesheet">
<script src="Scripts/jquery-1.9.1.min.js"></script>
<script src="Scripts/jquery.signalR-2.2.0.min.js"></script>
<script src="http://localhost:50000/signalr/hubs" type="text/javascript"></script>
<!-- HEAD ^^^ -->
<!-- Deleted stuff -->
<!-- BODY vvv -->
<script type="text/javascript">
$(function () {
var broadcaster = $.connection.loginHub;
console.log("broadcaster: " + broadcaster)
$.connection.hub.start().done(function () {
});
});
</script>
After re-creating my Frontend project and only installing the Microsoft.AspNet.SignalR.JS package I know receive this error in the console:
HTTP Error 404.0 - Not Found .... Requested URL
http://localhost:60000/signalr/negotiate?clientProtocol=1.5&connectionData=%5B%5D&_=1443076975867
http://localhost:60000 is the Frontend so I'm not sure why that is the requested URL.
When checking http://localhost:50000/signalr/hubs I get a website from ASP.NET SignalR JavaScript Library v2.2.0 and also when I print out broadcaster in the console it says broadcaster: [object Object] so some sort of connection seems to be established.
You probably need to set up the client to point to your actual hub location since by default it's the current server.
$.connection.hub.url = '<yourbackendurl>;
Do that before connection.hub.start is called and it should work for you.
In your Backend project you need to install these packages:
PM> Install-Package Microsoft.AspNet.SignalR
PM> Install-Package Microsoft.Owin.Cors
And its Startup class should be like this:
public void Configuration(IAppBuilder app)
{
app.UseCors(CorsOptions.AllowAll);
app.MapSignalR();
}
In your Frontend project you need to install this package:
PM> Install-Package Microsoft.AspNet.SignalR.JS
Also you can install SignalR for .NET client:
PM> Install-Package Microsoft.AspNet.SignalR.Client

JW Player not working with Meteor.js

I am trying to get JW Player to work inside Meteor.js. I have tried both cloud-hosted and self-hosted and have not been able to get the player to show. I'm not sure if there is a conflict using the two together. It seems that it should be pretty straight forward but I just can't get it to work. Any suggestions would be great.
Thanks
<head>
<title>Mysite</title>
<script src="http://jwpsrv.com/library/mytoken.js"></script>
</head>
<body>
<div id='my-video'></div>
<script type='text/javascript'>
jwplayer('my-video').setup({
file: 'http://localhost:3000/mymp3.mp3',
width: '640',
height: '360'
});
</script>
</body>
Here is an error message that I am getting:
Resource interpreted as Image but transferred with MIME type text/html: "http://167.206.59.228/2-2573/a348fe94-6cbf-458f-8d56-8b69e6091c42_25.152.50.88/5.2.992971814237535". 167.206.59.228/:1
Request URL:http://167.206.59.228/2-2573/ec00f5c8-61a2-493b-a2e6-943f52ac381f_25.152.26.118/5.2.5313433578703552
Request Method:GET
Status Code:200 OK
Request Headersview source
Accept:*/*
Accept-Encoding:gzip,deflate,sdch
Accept-Language:en-US,en;q=0.8
Cache-Control:max-age=0
Connection:keep-alive
Host:167.206.59.228
Referer:http://localhost:3000/
User-Agent:Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/27.0.1453.110 Safari/537.36
Response Headersview parsed
HTTP/1.1 200 OK
Server: PorchLight/6.0.20061.1302
Content-Type: text/html; charset=utf-8
Connection: Close
Content-Length: 1
Meteor does things differently than you'd expect. There is a Meteor way to build what you want. I've built a working demo based on your code:
https://github.com/michaelbishop/so-jwplayer
Let me try and explain step by step what I changed.
Meteor really likes templates. To do it the Meteor way, move the video div <div id='my-video'></div> to a template:
<template name="video">
<div id='my-video'></div>
</template>
This allows you to operate on the template and do things like setup events or run javascript.
Next, move the player code into a file with the extension of .js:
jwplayer('my-video').setup({
file: 'http://localhost:3000/mymp3.mp3',
width: '640',
height: '360'
});
You'll want the video player code to act upon the template after the page has been rendered and the library has been loaded like so:
Template.video.rendered = function () {
jwplayer('my-video').setup({
file: 'http://localhost:3000/mymp3.mp3',
width: '640',
height: '360'
});
};
There is a also a created method (Template.video.created), but it won't work because the page hasn't been loaded yet and so the javascript library hasn't been loaded yet. You need the jwplayer setup code to execute after the page has loaded and the library is available.
Finally, static files, like mymp3.mp3 need to be placed in the public folder. I found a open source mp3 and used that for the example.
Hope that helps! Good luck!

Using FileInfo to see when a file was updated... on another server

Hey guys I am pulling in a Vehicle Feed to an autodealer website for one of our clients. Every night at midnight(ish) the new XML file is uploaded to our FTP and it overwrites the current one. Currently he has two Identical websites and the file needs to be uploaded to both, I was looking into setting it up so both websites can use the same XML file so we can cut down on the risk of errors and for convince.
Pulling the file works great, both websites can read the XML file and have no issues displaying the inventory. The issue comes in when I try to display the date the file was last updated. I created a small snippet that reads the date the file was updated and displays "Last Update: and the date" but when I try and reference a non-local file I get a error that says "URI formats are not supported". Does anyone know of a way to do this or if its even possible?
what it currently is
FileInfo fileInfo = new FileInfo(Server.MapPath("~/feed/VEHICLES.XML"));
DateTime timeOfCreation = fileInfo.LastWriteTime;
what i tried
FileInfo fileInfo = new FileInfo("http://www.autodealername.com/feed/VEHICLES.XML");
DateTime timeOfCreation = fileInfo.LastWriteTime;
this was no good
This can be done via FTP, since you're using it already.
http://msdn.microsoft.com/en-us/library/system.net.ftpwebresponse.lastmodified.aspx
FileInfo uses information from the underlying file system which isn't available over HTTP. You'll need to think of some other way.
if you load the file in this way:
FileInfo fileInfo = new FileInfo("http://www.autodealername.com /feed/VEHICLES.XML");
most likely the file is retrieved to you by IIS or the webserver on that domain/site and this is not the same as opening the file from the file system directly.
I think you have two alternatives at least:
open the file from a network share like \\machinename\ShareName\FileName;
create a service endpoint on the remote server (WCF or XML web service) which gets in a file name and returns the information you need;
You can try using a WebRequest using the HEAD method and look for the Last-Modified header.
Here's the code I used...
var web = WebRequest.Create("http://cdn.sstatic.net/stackoverflow/img/sprites.png?v=4") as HttpWebRequest;
web.Method = "HEAD";
var response = web.GetResponse();
var lastModified = DateTime.Parse(response.Headers["last-modified"]);
Console.WriteLine(lastModified);
Here's what the http response looks like (from Fiddler)...
HTTP/1.1 200 OK
Server: nginx/0.8.36
Date: Wed, 23 Nov 2011 17:37:44 GMT
Content-Type: image/png
Connection: keep-alive
Cache-Control: max-age=604800
Last-Modified: Tue, 06 Sep 2011 21:44:29 GMT
ETag: "6237328de6ccc1:0"
Content-Length: 19706
X-Cache: HIT
Accept-Ranges: bytes
You could also add the updated field to the feed so you can get the last time it was updated from the feed itself.
RSS pubDate:
http://www.w3schools.com/rss/rss_tag_pubdate.asp
<?xml version="1.0" encoding="ISO-8859-1" ?>
<rss version="2.0">
<channel>
<title>W3Schools Home Page</title>
<link>http://www.w3schools.com</link>
<description>Free web building tutorials</description>
<!-- YOU COULD USE THIS -->
<pubDate>Thu, 27 Apr 2006</pubDate>
<item>
<title>RSS Tutorial</title>
<link>http://www.w3schools.com/rss</link>
<description>New RSS tutorial on W3Schools</description>
</item>
</channel>
</rss>
Atom updated:
http://www.atomenabled.org/developers/syndication/atom-format-spec.php#rfc.section.1.1
<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
<title>Example Feed</title>
<link href="http://example.org/"/>
<!-- YOU COULD USE THIS -->
<updated>2003-12-13T18:30:02Z</updated>
<author>
<name>John Doe</name>
</author>
<id>urn:uuid:60a76c80-d399-11d9-b93C-0003939e0af6</id>
<entry>
<title>Atom-Powered Robots Run Amok</title>
<link href="http://example.org/2003/12/13/atom03"/>
<id>urn:uuid:1225c695-cfb8-4ebb-aaaa-80da344efa6a</id>
<updated>2003-12-13T18:30:02Z</updated>
<summary>Some text.</summary>
</entry>
</feed>
Maybe try using the FileSystemWatcher Class, which can notify you when a file was changed, modified, etc. Take a look at it.
Good luck!

Resources