Home > database >  .net 6 api Redirect page cannot be found
.net 6 api Redirect page cannot be found

Time:02-01

I'm creating and testing my first api, using visual studio .net 6 core and EF core. Right now I'm just trying to make sure I have everything functioning. I have a single controller, and I want to redirect to an html page that gives general API usage info. When I run the debugger and the browser opens to https://localhost:xxxx I append /api/values to the end and this gets handled by my controller:

public class ValuesController : ControllerBase
{
    private readonly IWebHostEnvironment _env;
    public ValuesController(IWebHostEnvironment env)
    {
        _env = env;
    }

    // GET: api/<ValuesController>
    [HttpGet]
    public ActionResult Get() //was return type IEnumerable<string>
    {
        //return new string[] { _env.ContentRootPath, _env.WebRootPath }; //<- WebRootPath returns as null
        return Redirect("../Index.html");

    }
    //other stuff removed for brevity, just calling the above get function
}

In my code base directory I have the Index.html file created, and set to copy with a publish. But I can't get the redirect to this page to work.

The error I get is:

No webpage was found for the web address: https://localhost:xxxx/api/Index.html
HTTP ERROR 404

When I open Index.html from the file browser it renders properly in Chrome.

I have tried using Index.html, /Index.html, and ../Index.html in the redirect call all with the same error, though from different locations.

When I ran with the string array return I see that my ContentRootPath is the path to my project root (where Index.html exists), and that WebRootPath = null.

In launchSettings.json the applicationURL is listed a couple of times. The first is in iiSettings and given as http://localhost:xxxxx (does not seem to be used) and the second in profiles and given as https://localhost:xxxx;http://localhost:yyyy (I use the https port and getting the redirect in the controller to work, because the address bar shows localhost:xxxx/api/Index.html but the page is the error text above.

I have created a folder api in the project and put a copy of Index.html in there, still with the same error.

Here is my Program.cs app initialization:

var builder = WebApplication.CreateBuilder(args);
builder.Services.AddDbContext<ApiDbContext>();
builder.Services.AddDatabaseDeveloperPageExceptionFilter();
builder.Services.AddControllers();
builder.Services.AddHealthChecks();
var app = builder.Build();
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseAuthentication();
app.MapControllers();
app.Run();

How do I serve up a static page from my api?

CodePudding user response:

Ok, for some reason my googling got better after I posted this question. If anyone is looking here for a similar problem, I found the solution here.

Simply return a ContentResult object:

return new ContentResult
{
    Content = System.IO.File.ReadAllText(@"./Index.html"),
    ContentType = "text/html"
}; //new string[] { "value1", "value2" };

ControllerBase has a Content() method that will do this for you as well, but what I have above is working within my public ActionResult Get() method.

CodePudding user response:

"RedirectToAction("Directory");" You can try doing this, it will throw you directly to the Index page

  • Related