.net core 3.1 web.config with routing - reactjs

I tried publish my project on hosting, but I have some issues.
I have SPA (react) in same folder as .netCore(rest api).
I want SPA routing, thats why I set rule in web.config, but it is not working. When I remove handlers ("aspNetCore") section from config, it is works, but restApi doesnt.
Can anyone help me with web.config?
I just want routing with react and have working restapi on same domain myDomain.com/api.....
Thank you.
My web.config.
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<location path="." inheritInChildApplications="false">
<system.webServer>
<rewrite>
<rules>
<rule name="redirect all requests" stopProcessing="true">
<match url="^(.*)$" ignoreCase="false" />
<conditions logicalGrouping="MatchAll">
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" pattern="" ignoreCase="false" />
</conditions>
<action type="Rewrite" url="index.html" appendQueryString="true" />
</rule>
</rules>
</rewrite>
<aspNetCore processPath=".\Horseedo.Api.exe" stdoutLogEnabled="true" stdoutLogFile=".\logs\stdout" hostingModel="inprocess" />
<handlers>
<add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModuleV2" resourceType="Unspecified" />
</handlers>
</system.webServer>
</location>
</configuration>
<!--ProjectGuid: 654874ba-ab44-4982-a101-ee05ba73cc4e-->

Related

React: 404 Not Found with Proxy after deploying to IIS

I really need your help and advice on this, I have been searching for 2 weeks and I'am unable to resolve my proxy issues.
I'm setting up the below 'setupProxy.js' to bypass the CORS while requesting the API/Endpoints from the (Web Servers).
However, the proxy and Origin replacement only works from localhost 3000 when using the terminal to run it. For some reasons, It doesn't work after deploying to IIS, it keep getting "404 Not Found". Could you please tell me what should I do to fix the issue here?
setupProxy.js:
app.use('/GCM.Order.API',
createProxyMiddleware({
target:'https://gcm.dellsvc',
secure: false,
changeOrigin: true
})
);
Axios call:
let gcmpapi =
  "/GCM.Order.API/api/v1/orders/purchase-summary-with-details"
Terminal/Localhost:3000 result: (200 ok)
However, in IIS browse :8080 (http) : (404 Not Found)
web.config file:
<?xml version="1.0"?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="Rewrite Text Requests" stopProcessing="true">
<match url=".*" />
<conditions>
<add input="{HTTP_METHOD}" pattern="^GET$" />
<add input="{HTTP_ACCEPT}" pattern="^text/html" />
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
</conditions>
<action type="Rewrite" url="/index.html" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
i had exactly the same problem i was dealing with for 2 weeks. Finally I have a satisfactory solution.
http-proxy-middleware or proxy in package.json is intended (in my opinion) for development purposes only.
What you have created is absolutely correct, but you can only use it in development.
For production settings, is only handled at the url rewrite level. And I think it's logical. It's separate from the code, and if there's an error in the code (eg a bad endpoint pointing to localhost), it won't affect production.
The solution is to modify the url rewrite in the web.config file, which is stored securely in IIS.
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="Reverse Proxy to api" stopProcessing="true">
<match url="^api/(.*)" />
<action type="Rewrite" url="https://api.example.com/{R:0}" />
</rule>
<rule name="Reverse Proxy to pusher" stopProcessing="true">
<match url="^pusher/(.*)" />
<action type="Rewrite" url="https://pusher.example.com/{R:0}" />
</rule>
<rule name="React Routes" stopProcessing="true">
<match url=".*" />
<conditions logicalGrouping="MatchAll">
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
<action type="Rewrite" url="/" />
</rule>
</rules>
</rewrite>
<httpProtocol>
<customHeaders>
<add name="Access-Control-Allow-Origin" value="*" />
</customHeaders>
</httpProtocol>
</system.webServer>
</configuration>

React Deployment in Azure DevOps

When i try to navigate to a particular route or page the app says "The resource you are looking for has been removed, had its name changed, or is temporarily unavailable." and also the fonts are not loaded, Only the initial page is loaded no other navigation is happening. Can anyone help me out?
Font not loaded and the path font after npm run build is static/media/font_name
You need a web.config file in your public folder to implement client side routing. React uses client side routing and your server should know to allow that.
Add a web.config file in your public directory with the content below:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="Static Assets" stopProcessing="true">
<match url="([\S]+[.](html|htm|svg|js|css|png|gif|jpg|jpeg))" />
<action type="Rewrite" url="/{R:1}"/>
</rule>
<rule name="ReactRouter Routes" stopProcessing="true">
<match url=".*" />
<conditions logicalGrouping="MatchAll">
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
<action type="Rewrite" url="/index.html" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
create .htaccess file in public folder with the below code:
RewriteEngine On
RewriteRule "^[^\.]+$" "index.html"

How to add rewrite rule for multiple applications hosted in one Azure App Service

I am hosting four different applications in one Azure App Service - 2 static reactjs web sites and 2 .NET CORE APIs.
The applications are set in different virtual directories:
/
/api
/admin
/admin/api
I am trying to write a redirect rule so that I can use the react-router routing for the static sites but also be able to use the .NET Core API routing.
This is what I tried for the admin route but the only thing that works is the routing for the static site, not the API
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="ReactRoutes" stopProcessing="true">
<match url="admin/(.*)" />
<conditions logicalGrouping="MatchAll">
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
<add input="{REQUEST_URI}" matchType="Pattern" pattern="api/(.*)" negate="true" />
</conditions>
<action type="Rewrite" url="/admin/" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
I fixed the rules for the admin by writing the following web.config which I hosted in /admin root
<?xml version="1.0"?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="Rewrite Admin React Routes" enabled="true" stopProcessing="true">
<match url=".*" />
<conditions logicalGrouping="MatchAll">
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_URI}" pattern="^/admin/api(.*)$" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
<action type="Rewrite" url="/admin/index.html" logRewrittenUrl="false" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>

Azure App Service Deploy does not deploy web app successfully

I am a newbie to React
I created a node app service in the Azure Portal using the Node JS Empty web App Starter web app.
When I go to the URL I see
Hello, world!
Which I note is in the contents of server.js
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, { 'Content-Type': 'text/html' });
res.end('Hello, world!');
}).listen(process.env.PORT || 8080);
I see that web.config refers to server.js
<configuration>
<system.webServer>
<handlers>
<!-- indicates that the app.js file is a node.js application to be handled by the iisnode module -->
<add name="iisnode" path="server.js" verb="*" modules="iisnode" />
</handlers>
<rewrite>
<rules>
<!-- Don't interfere with requests for logs -->
<rule name="LogFile" patternSyntax="ECMAScript" stopProcessing="true">
<match url="^[a-zA-Z0-9_\-]+\.js\.logs\/\d+\.txt$" />
</rule>
<!-- Don't interfere with requests for node-inspector debugging -->
<rule name="NodeInspector" patternSyntax="ECMAScript" stopProcessing="true">
<match url="^server.js\/debug[\/]?" />
</rule>
<!-- First we consider whether the incoming URL matches a physical file in the /public folder -->
<rule name="StaticContent">
<action type="Rewrite" url="public{REQUEST_URI}" />
</rule>
<!-- All other URLs are mapped to the Node.js application entry point -->
<rule name="DynamicContent">
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="True" />
</conditions>
<action type="Rewrite" url="server.js" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
When I deploy my static react website files to the site, I can see the files are there but the web site itself does not change when I go to the URL.
I think what I need is for the website to start src\app.js instead of server.js
How do I do that?
I tried replacing server.js with ./src/App.js but then the App.js just displays as text.
This Burke Holland shows how to set up the pipeline ( near the bottom)
And also links to this article by Tony Patrina that shows how to set up web.config.
As follows;
<?xml version="1.0"?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="React Routes" stopProcessing="true">
<match url=".*" />
<conditions logicalGrouping="MatchAll">
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
<add input="{REQUEST_URI}" pattern="^/(api)" negate="true" />
</conditions>
<action type="Rewrite" url="/" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>

IIS rewrite condition for React MVC App

I have created a create-react-app and am trying to publish to a production site.
My goal is to have all calls to /api/* go through to the MVC Server application's controller.
Also serve any static files from /content/* and /static/*
All other calls should serve the 1 file /index.html (which is where my react files live and they will handle the routing.
I am using the IIS Rewrite module, and I am trying to use the requested URL "Does Not MAtch the pattern" with regexp.
MY current IIS setup ignores the "do not rewrite if pattern matches" and rewrites everything to index.html
Here is the IIS setup:
and here is the web.config. I'm not sure what I am doing wrong.
<rewrite>
<rules>
<rule name="React Rewrite" enabled="true" patternSyntax="ECMAScript">
<match url="(.*)/api/(.*)|(.*)/content/(.*)|(.*)/static/(.*)" negate="true" />
<action type="Rewrite" url="/index.html" />
<conditions>
</conditions>
</rule>
</rules>
</rewrite>
I've solved it by using the Conditions. My solution is:
<rewrite>
<rules>
<rule name="React Rewrite" enabled="true" patternSyntax="ECMAScript">
<match url="(.*)" negate="false" />
<action type="Rewrite" url="/index.html" />
<conditions logicalGrouping="MatchAll">
<add input="{REQUEST_URI}" pattern="^(/api/)" negate="true" />
<add input="{REQUEST_URI}" pattern="^(/content/)" negate="true" />
<add input="{REQUEST_URI}" pattern="^(/static/)" negate="true" />
</conditions>
</rule>
</rules>
</rewrite>

Resources