Reputation: 27
The default route in asp.net mvc core 2.1 is http://localhost:5000/controller/action/id
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
There are some way to create a new route that I can use a parameter before "localhost" as shown below?
http://myparameter.localhost:5000/controller/action/id
Thank you
Upvotes: 0
Views: 1022
Reputation: 27
First of all I'd like to thank all help.
If someday someone needs to do it, I developed the following solution with the klabranche response informations.
Startup.cs
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
....
using (StreamReader iisUrlRewriteStreamReader =
File.OpenText("RewriteUrlsFiles/iisUrlRewrite.xml"))
{
var options = new RewriteOptions()
.AddIISUrlRewrite(iisUrlRewriteStreamReader);
app.UseRewriter(options);
}
....
}
iisUrlRewrite.xml - Rewrite https://Mustang.localhost:5001/forsale?anyquerystring=anyvalue for https://localhost:5001/cars/Mustang/forsale?anyquerystring=anyvalue
<rewrite>
<rules>
<rule name="test1" stopProcessing="true">
<match url=".*" />
<conditions>
<add input="{HTTP_HOST}" pattern="^((.+.)?).localhost:5001" />
</conditions>
<action type="Rewrite" url="https://localhost:5001/Cars/{C:1}{REQUEST_URI}" />
</rule>
</rules>
</rewrite>
Controller
[Route("Cars")]
public class CarsController : Controller
{
[Route("{nameCar}")]
public IActionResult Index(string nameCar)
{
return View("Views/Home/CarsView.cshtml");
}
[Route("{nameCar}/forsale")]
public IActionResult ForSale(string nameCar)
{
return View("Views/Home/CarsForSaleView.cshtml");
}
}
Upvotes: 0
Reputation: 21098
You can't do this with an mvc route and/or your web framework. You have to do this at the web server and dns level. It's called subdomains.
The DNS has to be created as a wildcard subdomain (*.domainname) so you can have as many as you want or manually for each if you know you are going to have a few (a.domain, b.domain) so that domain (localhost) knows that anything (*) before the domain name is still directed at your server.
You then have to setup your web server to know how to take each subdomain and send it to the correct website.
If you have two subsites a.localhost & b.localhost you need to add two sites to IIS (assuming IIS since this is asp.net) with the sitenames of a.localhost and b.localhost.
You will have to point the site's physical path to each's folder (a & b), leave the binding alone/default (if I remember right) to have it point to localhost (or you domain name) and the host has to be a.localhost & b.localhost respectively.
Since you are doing this on localhost, I'm going to assume you are doing this on a developer machine. The hosts file can mimic your DNS need on your local machine. Change your hosts file (C:\Windows\System32\drivers\etc) to add your a.localhost & b.localhost to your local ip address to ensure they resolve.
127.0.0.1 a.localhost
127.0.0.1 b.localhost
If you are working on a local dev box and you are using IIS express you can still do the subdomain setup as well with it. Instead of doing the steps for IIS you would open your applicationhost.config file in your .vs\config folder and manually find & edit the bindings for your application. In your sample case it would be something like <binding protocol="http" bindingInformation="*:5000:localhost">
add two new bindings for a and b <binding protocol="http" bindingInformation="*:5000:a.localhost">
And.... once you get this working your routes in MVC are ignorant to the subdomain. You don't change them.
Upvotes: 4