简体   繁体   中英

Routing for Subsite on ASP.net core 2.1 MVC

I'm attempting to create a subsite (***.com/rentals), and continually get a server error when navigating to it's uri. I created the folder 'rentals' as a subfolder in wwwroot, created a route as follows:

 app.UseMvc(routes =>
            {
                routes.MapRoute(
                    name: "default",
                    template: "{controller=Home}/{action=Index}/{id?}");
                routes.MapRoute(
                    name: "rentals",
                    template: "{controller=Rentals}/{action=~/rentals/Index}/{id?}");

            });

and generated a new controller with the following code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;

// For more information on enabling MVC for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860

namespace hillandco.Controllers
{
    public class RentalsController : Controller
    {
        // GET: /<controller>/
        public IActionResult Index()
        {
            return View();
        }
    }
}

I created an Index.html file, and unfortunately navigating to the subsite root using 'https://***.com/rentals' still serves an error. Any help with what I'm missing here would be fantastic!

Solved. Routing and controller action worked correctly as is; the issue was that I attempted to use a subfolder in wwwroot. When I created a folder under 'Views' instead I was able to use the subsite just fine.

This behaviour seems strange, but I suppose makes sense from an MVC perspective and the way URI's work in asp.net-core.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM