Display View in folder without Controller or Action in ASP.net MVC

后端 未结 4 1542
春和景丽
春和景丽 2021-01-15 17:29

I would like to display the view in the folder if no controller/action matches.

For example www.site.com/Home/Index, if I have the normal default route {controller}/

4条回答
  •  天命终不由人
    2021-01-15 18:15

    Never tried this, but here's a thought. You can setup constraints for the routes, and thus you should be able to create a route matching "{folder}/{file}" where you constraint them to valid values (you can google this, or seach her on SO), and set it to run on a FileController (arbitrary name) with some default action. Then, in that action, simply return the desired view. Something like:

    public class FileController : Controller {
        public ActionResult Default(string folder, string file) {
            return View(folder + "/" + file);
        }
    }
    

提交回复
热议问题