I have an asp.net website. The Admin panel is located here:
mysite.com/manager
I want to change this to:
mysite.com/admin
There is no file named manager in the website files and I'm unsure how to make this change or locate the manager source file.
Firstly, it's unlikely to be related to a database in anyway.
Sounds like your project is using MVC and Routing:
The ASP.NET Routing module is responsible for mapping incoming browser requests to particular MVC controller actions
If so, your web project will have a folder called "Controllers" and within that there will be a file called "ManagerController.cs".
Within this file, you'll see a method that looks like:
public virtual ActionResult Index()
{
// some code maybe
return View();
}
This is what will be called when that URL is hit.
You may also want to search for a file called RouteConfig.cs, which is located in the App_Start folder, where any non-standard routes will be configured.
To make the change, you can copy and rename ManagerController.cs to AdminController.cs and MVC will work it's magic as the default routing will search for that file instead.
Related
I have an ASP.NET MVC app, in which I use a custom authorization filter throughout several parts of the app.
[MyCustomAuthorize]
class MyController
{
}
This has worked well for a long while.
Now I want to add another piece to the app which is basically a pre-built React app - a subfolder structure containing an index.htm file, JS, CSS, and other resource files.
When a user navigates to this sub-app, I want them to be sent through the same authorization process implemented in my custom authorization attribute. But naturally, since it's a static .htm file, I have no way of applying any attribute to it.
I tried to resolve this by placing it behind an MVC controller:
using MyApp.Filters;
using System.Web.Mvc;
namespace MyApp.Controllers
{
[MyCustomAuthorize]
public class SubAppController : Controller
{
public ActionResult Index()
{
return File("~/SubApp/index.htm", "text/html");
}
}
}
but it seems that what happens if I navigate to https://mydomain/SubApp/ is that IIS finds the index.htm file and serves that up instead of the request going through the controller.
If I rename the index.htm to something else and try to serve it this way:
using MyApp.Filters;
using System.Web.Mvc;
namespace MyApp.Controllers
{
[MyCustomAuthorize]
public class SubAppController : Controller
{
public ActionResult Index()
{
return File("~/SubApp/subapp.htm", "text/html");
}
}
}
then it seems IIS sees the https://mydomain/SubApp/ request as an attempt to access the physical SubApp/ folder, and returns a 403 response.
I realize I could get around these two issues by physically placing the sub-app contents in a folder with a different name from the controller, but that would introduce yet another problem where all of the sub-app's resource files (CSS, JS, etc.) are no longer in a sub-path of the path where the sub-app is being accessed (i.e. the browser would be accessing https://mydomain/SubApp/ and the .css would be in https://mydomain/SubAppFiles/styles.css). I would like to avoid that situation if possible, and have all of the resources (both physically and conceptually) contained within the SubApp/ folder.
Is there a relatively simple way to get this to work nicely without too much overhaul to the main app?
I think the ideal situation would be to find some way to have IIS not try to handle the request to https://mydomain/SubApp/ (just that one path and nothing else) and allow the ASP.NET app to handle it, but as of yet, I've been unsuccessful in finding a way to do that.
I'm trying to deploy my ASP.NET Core MVC Web App with Web API, i.e. I have both MVC and API controllers in the same folder.
It works fine on localhost but on IIS when I create a Virtual Directory, the path gets added to the domain.
I can find it using window.location.pathname
I can append the 'api/Get' and it works like (questions is my virtual directory)
http://example.com/questions/api/Question/GetAll
But when I navigate to other pages then then controller name also gets appended and then it causes issues.
e.g. if I navigate to the 'Question' page (QuestionController), the URL becomes
http://example.com/questions/newquestion/api/Question/Create
instead of
http://example.com/questions/api/Question/Create
How can I fix it?
Here is my Asp.Net core api.
[ApiController]
public class ScheduleController : ControllerBase
{
[HttpGet]
public List<PathologistSchedule> GetPathologistScheduleByDate(DateTime taskDate)
{
return pathologistRepository.GetPathologistScheduleByDate(taskDate).ToList();
}
}
I call this api from PathologistScheduleController's view using jquery.
Here's the error I get:
GET http://localhost:51434/PathologistSchedule/api/Schedule/?sort=&group=&filter=&taskDate=2020-11-13T21%3A16%3A47.507Z 404 (Not Found)
TIA.
A
If you have API and MVC projects in one solution you have to config your solution to run multiple projects.
You can use route attribute like this for each of your APIs
[Route("~/api/Question/GetAll")]
will give you Url http://example.com/api/Question/GetAll.
Or
[Route("~/api/Question/Create")]
will give Url http://example.com/api/Question/Create.
And it will not depend on the controller name or folder.
UPDATE because of the question update:
Use this code please:
public class ScheduleController : ControllerBase
{
[Route("~/api/Schedule/GetPathologistScheduleByDate/{taskDate?}")]
public List<PathologistSchedule> GetPathologistScheduleByDate(DateTime taskDate)
{
return pathologistRepository.GetPathologistScheduleByDate(taskDate).ToList();
}
}
for testing try this route in your browser:
http://localhost:51434/api/Schedule/GetPathologistScheduleByDate/2020-11-13T21%3A16%3A47.507Z
But basically for APIs you don't need to use any controller or action name. You can use any names you like, for example:
[Route("~/api/Pathologist/GetSchedule/{taskDate?}")]
or
[Route("~/api/GetPathologistSchedule/{taskDate?}")]
or even
[Route("~/api/{taskDate?}")]
The route just should be unique.
I added a variable in the 'appsettings.json' and 'appsettings.Development.json' called baseURL and had 'appsettings.json' set to '/VirtualDirectoryName/' and kept the one in 'appsettings.Development.json' as '/'.
Appended this variable when calling APIs.
In my ASP.NET Core project I am trying to serve an html file like this:
public IActionResult Index()
{
return File("c:/path/to/index.html", "text/html");
}
This results in an Error:
FileNotFoundException: Could not find file: c:/path/to/index.html
Pasting the path from the ErrorMessage into my browser I am able to open the file, so the file is clearly there.
The only way I have been able to serve the file is placing it in wwwroot in the project folder and serving it like this:
public IActionResult Index()
{
return File("index.html", "text/html");
}
I have already changed the folder I serve static files from using app.UseStaticFiles(options) (which works), so I figured the Controller would use that folder as default, but it keeps looking in wwwroot.
How can I serve a file that is placed outside of wwwroot, or even outside the project, from a Controller?
You need to use PhysicalFileProvider class, that is an implementation of IFileProvider and is used to access the actual system's files. From File providers section in documentation:
The PhysicalFileProvider provides access to the physical file system. It wraps the System.IO.File type (for the physical provider), scoping all paths to a directory and its children. This scoping limits access to a certain directory and its children, preventing access to the file system outside of this boundary. When instantiating this provider, you must provide it with a directory path, which serves as the base path for all requests made to this provider (and which restricts access outside of this path). In an ASP.NET Core app, you can instantiate a PhysicalFileProvider provider directly, or you can request an IFileProvider in a Controller or service's constructor through dependency injection.
Example of how to create a PhysicalFileProvider instance and use it:
IFileProvider provider = new PhysicalFileProvider(applicationRoot);
IDirectoryContents contents = provider.GetDirectoryContents(""); // the applicationRoot contents
IFileInfo fileInfo = provider.GetFileInfo("wwwroot/js/site.js"); // a file under applicationRoot
I am trying to add BlogEngine.NET(web) to my existing MVC5 project (which is running as an azure website) partly based on these instructions. I have downloaded/extracted the entire web project folder and included it as a folder to my existing MVC Project.
My controller has the below code
public ActionResult Blog()
{
var path = Server.MapPath("~/BlogEngine/default");
return View(path);
}
But, I receive the error:
....BlogEngine\default' or its master was not found or no view engine
supports the searched locations.
by reading this, I understand why this error occurs. I just want some pointers on how to add the Blog engine to my existing project.
EDIT:
this is exactly what I want to achieve, but azure is not helping(the whole point of me choosing to use azure websites instead of AzureVMs is to shy away from the responsibility of keeping that VM up and running), so looking at the other options at hand, like tweaking my MVC.
goto manage.windowsazure.com
select the webapp
Navigate to the Configuration Tab
Scroll Down to Virtual Applications and Directories
Add the Path of the Directory there and Select the Application checkbox.
Hit Save.
Background
I have a PDF file located (under my Project) in an Assets > Documents folder:
When my application gets deployed, it gets deployed to a particular folder on the domain. For example, http://www.domain.com/MyAppFolder. I want to be able to access this PDF file by linking to http://www.domain.com/MyAppFolder/Assets/Documents/EZTrac_UserGuide_NewSys.pdf
Problem
I can't seem to get the routing correct for this, as it keeps trying to route this request to a controller. Here is the modification I made to the routing:
routes.IgnoreRoute("MyAppFolder/Assets/Documents/EZTrac_UserGuide_NewSys.pdf");
But this is the result that I get:
The IControllerFactory
'EZTrac.DependencyResolution.ControllerFactory'
did not return a controller for a
controller named 'Assets'.
Try removing the MyAppFolder from your routes.
routes.IgnoreRoute("Assets/Documents/EZTrac_UserGuide_NewSys.pdf");