1

I've a ASP.NET MVC 4 Project and I have a subdirectory "test" in the root dir and it contains a index.html

I want wenn the clean URL is called that the index.html is returned

when I call

http://localhost:8080/

that I get the Content returned from my "test\index.html"

4
  • are you not using razor engine's .cshtml pages? Commented Nov 28, 2016 at 17:56
  • 1
    You are looking for routing, this article from MSDN shows you how to register a default route (what you are looking for): msdn.microsoft.com/en-us/library/cc668201.aspx#Anchor_3 Commented Nov 28, 2016 at 17:58
  • @Aravind no I am using Angular 2 Commented Nov 28, 2016 at 18:00
  • @squadwuschel in that case (with angular) this link may be more useful stackoverflow.com/a/24071300/3061857 Commented Nov 28, 2016 at 18:01

2 Answers 2

3

The RouteConfig class in the App_Start sub-directory should be adjusted so that the default Controller is Test. Place your 'test' directory within the Views directory and, if you haven't already, create a TestController. RouteConfig should look like this:

public class RouteConfig
{
    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "Test", action = "Index", id = UrlParameter.Optional }
        );
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

thats what I am looking for I've added a ActionResult in my TestController which returns => { return new FilePathResult("~/test/index.html", "text/html"); }
1

The easiest way is to put your "test/index.html" as default document in IIS.

If you really want to do it programmatically you'll have to create an action to handle your request and map your default route to it.

Route:

routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "StaticFile", action = "Index", id = UrlParameter.Optional }
        );

Controller/Action:

public class StaticFileController : Controller
{
    // GET: StaticFile
    [AllowAnonymous]
    public FileContentResult Index()
    {
        string imageFile = System.Web.HttpContext.Current.Server.MapPath("~/Test/Index.html");
        byte[] bytes = System.IO.File.ReadAllBytes(imageFile);

        return File(bytes, "text/html");
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.