How to change Swagger-Net root url from "/swagger" to root? - swagger-net

I"m using swagger-net. By default, the swagger UI will be "/swagger", how do I change it to root?
Feel like what I'm doing now is a hack
public class HomeController : Controller
{
public ActionResult Index()
{
return Redirect("/swagger");
//return View();
}
}

Found a solution. Added the below route mapping to WebApiConfig.cs
config.Routes.MapHttpRoute(
name: "Swagger UI",
routeTemplate: "",
defaults: null,
constraints: null,
handler: new RedirectHandler(SwaggerDocsConfig.DefaultRootUrlResolver, "swagger/ui/index"));

Related

ASP.NET MVC API controller not hitting

I have added an ASP.NET Web API to an ASP.NET MVC 5 web application project developed in Visual Studio 2019.
WebApiConfig:
public class WebApiConfig {
public static void Register(HttpConfiguration config) {
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
}
}
Global.asax.cs:
public class MvcApplication : HttpApplication {
protected void Application_Start() {
AreaRegistration.RegisterAllAreas();
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
GlobalConfiguration.Configure(WebApiConfig.Register);
BundleConfig.RegisterBundles(BundleTable.Bundles);
}
}
Api Controller Method:
// GET: api/Web
public IEnumerable<string> Get() {
return new string[] { "value1", "value2" };
}
When I hit https://localhost:44324/api/web the browser gives this error:
The resource cannot be found.
Description: HTTP 404. The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable. Please review the following URL and make sure that it is spelled correctly.
Requested URL: /api/web
In MVC 5 routing you can try using attributing routing to overcome this error.
The solution is shown below.
API Controller Method:
//write this above the class and below namespace
[ApiController]
[Route("[controller]")]
// GET: api/Web
[HttpGet]
public IEnumerable<string> Get() {
return new string[] { "value1", "value2" };
}
When you hit https://localhost:44324/api/web but here "Web" is controller id it's not then you have to mention that first and then the method the browser will not give you an error:

ASP.NET CORE 5 API controller not working

Added an API controller to the project and it does not work. I get 404.
[Route("api/hlth")]
[ApiController]
public class hlth : ControllerBase
{
// GET: api/<hlth>
[HttpGet]
public IEnumerable<string> Get()
{
return new string[] { "value1", "value2" };
}
// GET api/<hlth>/5
[HttpGet("{id}")]
public string Get(int id)
{
return "value";
}
}
Turns out I need to add
app.MapControllers();
that for some reason is not included in the default project configuration.

Routing error with default url in ASP.NET MVC 6

I have a routing problem in an MVC 6 web application : when I set route parameter in the controller used by default, application send a 404 error.
My routing configuration :
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Dashboard}/{action=Index}/{id?}");
});
My dashboard controller (application works) :
[Authorize]
public class DashboardController : Controller
{
public DashboardController()
{ }
[HttpGet]
public IActionResult Index() => View(new IndexViewModel());
}
Same dashboard controller (application responds a 404 error) :
[Authorize]
[Route("[controller]")]
public class DashboardController : Controller
{
public DashboardController()
{ }
[HttpGet]
[Route("[action]")]
public IActionResult Index() => View(new IndexViewModel());
}
The reason that this is occurring is that routes specified via routes.MapRoute only apply to controllers that are not using attribute based routing. Since your second example is using attribute based routing that controller can only be reached via the route specified in the attribute. So it can only be reached at /Dashboard/Index

Default Route does not work after upgrading to MVC5 with Attribute Routing

After upgrading from MVC4 with AttributeRouting.net to MVC5 with MVC5's attribute routing, I can't seem to get the default route working so that http://server defaults to http://server/home/index . Browsing directly to /home/ or /home/index works fine.
For route config, I have this:
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapMvcAttributeRoutes();
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
The HomeController declaration looks like this:
// Controller
[RoutePrefix("home")]
public class HomeController : MvcControllerBase
....
// Action
[HttpGet, Route, Route("index")]
public ActionResult Index()
{
.....
I'm not sure where else to check. I've commented out everything in Global and disabled all WebActivator-activated items.
And ideas? The response is 404 with no exception being thrown.
Ah.. got it!
Based on Kiran's answer to : Specify default controller/action route in WebAPI using AttributeRouting
I changed HomeController to:
// Controller
public class HomeController : MvcControllerBase
....
// Action
[HttpGet, Route, Route("home"), Route("home/index")]
public ActionResult Index()
{
....
And I got rid of the MVC Config default.

MVC2 & Ninject2 - Controllers not resolving dependency

I foolishly decided to try something new on a Friday job!
So I have used NuGet to add Ninject.Web.Mvc 2.2.x.x to my .Net MVC2 project.
I've altered my Global.asax.cs
using System.Web.Mvc;
using System.Web.Routing;
using IntegraRecipients;
using Mailer;
using Ninject;
using Ninject.Web.Mvc;
using Ninject.Modules;
namespace WebMailer
{
public class MvcApplication : NinjectHttpApplication
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.IgnoreRoute("favicon.ico");
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Mail", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
}
protected override void OnApplicationStarted()
{
AreaRegistration.RegisterAllAreas();
RegisterRoutes(RouteTable.Routes);
}
protected override IKernel CreateKernel()
{
return new StandardKernel(new INinjectModule[] { new MailModule()});
}
internal class MailModule : NinjectModule
{
public override void Load()
{
Bind<IMailing>().To<Mailing>();
Bind<IMailingContext>().To<MailingContext>();
Bind<IRecipientContext>().To<RecipientContext>();
}
}
}
}
and I've created a controller like so...
using System.Linq;
using System.Web.Mvc;
using WebMailer.Models;
namespace WebMailer.Controllers
{
[ValidateInput(false)]
public class MailController : Controller
{
private readonly IMailingContext _mailContext;
private readonly IRecipientContext _integraContext;
public MailController(IMailingContext mail,IRecipientContext integra)
{
_mailContext = mail;
_integraContext = integra;
}
public ActionResult Index()
{
return View(_mailContext.GetAllMailings().Select(mailing => new MailingViewModel(mailing)).ToList());
}
}
}
But the controller is still insisting that
The type or namespace name 'IRecipientContext' could not be found (are you missing a using directive or an assembly reference?)
and
The type or namespace name 'IMailingContext' could not be found (are you missing a using directive or an assembly reference?)
My google-fu has failed me and I really hope this is just a silly typo/missing line thing
Thanks in advance
P
Ninject does not change the way assemblies are compiled! It deos not magically add references to other assemblies or add using directives. If you are using interfaces from other assemblies you have to add a using directive and a reference to this assembly.
All Ninject is about is to wire up your application at runtime.
I am have what appears to be a similar problem.
I have a simple WPF Window project with the compiled Ninject.dll linked in. However, the following is giving me errors...
using Ninject;
namespace CatalogueManager
{
public class ServiceLocator
{
public IMainWindowViewModel GetMainWindowViewModel()
{
return Kernel.Get<IMainWindowViewModel>();
}
static IKernel Kernel;
static ServiceLocator()
{
Kernel = new StandardKernel(new NinjectConfiguration());
}
}
}
In particular, "Ninject" namespace and IKernel are prompting the compile time message "type or name space 'X' not found..."