I have an asp.net web site that won't seem to resolve to Default.aspx when initially loading. When I debug on my local machine it loads Default no problem. Unless I try to navigate to "localhost:#####/". Then it gives a 404 error. When I deploy it to a staging server, give it a virtual path "mywebapp", and load it from "mydomain.com/mywebapp" it gives a 404 as well. I have set Default.aspx to the top of the list for Default Document in IIS. If I navigate to "mydomain.com/mywebapp/default" the site loads just fine. Any suggestions? I would paste code but it is a large website and I quite honestly am not sure what I'm looking for anymore.
EDIT:
In my site I am also using DataTables for display and edit of data. In the ajax calls I was previously able to call the controller by using urls such as:
api/MyController/idvalue
but since uncovering this I had to go back and preface the urls to get them to work:
mywebapp/api/MyController/idvalue
Controller:
public class MyController : ApiController
{
[Route("api/MyContoller/{idvalue}")]
[HttpGet]
[HttpPost]
public IHttpActionResult MyControllerMethod(intidvalue)
{
}
}
WebApiConfig:
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
}
}
RouteConfig:
public static class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
var settings = new FriendlyUrlSettings();
settings.AutoRedirectMode = RedirectMode.Permanent;
routes.EnableFriendlyUrls(settings);
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
//routes.MapRoute(
// name: "Default",
// url: "{controller}/{action}/{id}",
// defaults: new { action = "Index", id = UrlParameter.Optional }
// );
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
}
Found the solution. In my RouteConfig.cs if I comment out the line:
controller = "Home",
it works just fine.
public static class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
var settings = new FriendlyUrlSettings();
settings.AutoRedirectMode = RedirectMode.Permanent;
routes.EnableFriendlyUrls(settings);
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
//routes.MapRoute(
// name: "Default",
// url: "{controller}/{action}/{id}",
// defaults: new { action = "Index", id = UrlParameter.Optional }
// );
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new
{
//controller = "Home",
action = "Index",
id = UrlParameter.Optional
}
);
}
}
However, I'm not completely sure why this is. So if anyone is able to explain it to me that would be great!
Related
I have an MVC5 application using ASP Identity 2 and Fluentsecurity with the following methods in the Home controller which handle the log in page and logging out:
public ActionResult Login(string returnUrl)
{
this.ViewBag.ReturnUrl = returnUrl;
return this.View();
}
public ActionResult LogOff()
{
this.AuthenticationManager.SignOut();
return this.RedirectToAction("Login", "Home");
}
This works absolutely fine.
I now need to add an area into my application and set up routing accordingly. I have added a sub folder called 'Admin' under the 'Areas' folder and placed the relevant controllers and views in there in the appropriate subfolders.
I have then set up the routing as follows in Global.asax:
private static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Default",
"{controller}/{action}/{id}",
new { controller = "Home", action = "Index", id = UrlParameter.Optional },
new[] { "AppName.Web.Controllers " });
routes.MapRoute(
"Admin",
"Admin/{controller}/{action}/{querydata}",
new { querydata = UrlParameter.Optional },
new[] { "AppName.Web.Areas.Admin.Controllers" });
}
Which is then called as below:
private void Application_Start(object sender, EventArgs e)
{
AreaRegistration.RegisterAllAreas();
GlobalConfiguration.Configure(WebApiConfig.Register);
SecurityConfigurator.Configure(SecurityConfig.Configure);
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
FilterConfig.RegisterGlobalApiFilters(GlobalConfiguration.Configuration.Filters);
RegisterRoutes(RouteTable.Routes);
ControllerBuilder.Current.DefaultNamespaces.Add("AppName.Web.Areas.Admin.Controllers");
}
If i then run the application, routing is working as expected in my area and the main application, apart from it seems to have broken the Login and LogOff methods in the aforementioned Home controller, now giving a 404 error when trying to access them. The index method of the same Home controller works fine though.
In case it's related to Fluentsecurity, this is how security is configured:
internal static void Configure(ConfigurationExpression configuration)
{
configuration.GetAuthenticationStatusFrom(() => HttpContext.Current.User.Identity.IsAuthenticated);
configuration.GetRolesFrom(GetRoles);
configuration.For<HomeController>(x => x.Login(default(string))).DenyAuthenticatedAccess();
configuration.For<HomeController>(x => x.Index()).DenyAnonymousAccess();
configuration.For<HomeController>(x => x.LogOff()).AllowAny();
//Admin Home
configuration.For<Areas.Admin.Controllers.HomeController>(x => x.Index()).DenyAnonymousAccess();
}
Any ideas where I may be going wrong?
private static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Admin",
"Admin/{controller}/{action}/{querydata}",
new { querydata = UrlParameter.Optional },
new[] { "AppName.Web.Areas.Admin.Controllers" });
routes.MapRoute(
"Default",
"{controller}/{action}/{id}",
new { controller = "Home", action = "Index", id = UrlParameter.Optional },
new[] { "AppName.Web.Controllers " });
}
How to display custom url and not based on controller and action names?
My routing looks like
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 = "Home", action = "Index", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "productfeature",
url: "cn/pendrive/18-gb",
defaults: new { controller = "Product", action = "Feature", id = UrlParameter.Optional }
);
}
}
and my link button looks like
<li>#Html.RouteLink("Product Feature", "productfeature")</li>
I get url as the Url mentioned in the code but i also 404 error.
http://localhost:9090/cn/pendrive/18-gb
How to go for static urls? Basically my aim is to no show controllers and action names in url.
Put your custom route before the default one:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "productfeature",
url: "cn/pendrive/18-gb",
defaults: new { controller = "Product", action = "Feature", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
I have RouteConfig
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "EmployerDefault",
url: "{lang}/employer",
defaults: new { lang = "ru", controller = "Employer", action = "Index" }
);
}
}
and Controller
public class EmployerController : Controller
{
public ActionResult Index()
{
return View("EmployerMaster");
}
}
When i go to link /employer , i get HTTP 404.0 - Not Found ,
but when i try to get /ru/employer it's OK.
I want that /employer and /ru/employer links refer to one page.
Why it's happens ? How can i fix that ?
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "EmployerDefault",
url: "/employer",
defaults: new { lang = "ru", controller = "Employer", action = "Index" }
routes.MapRoute(
name: "EmployerWithLang",
url: "{lang}/employer",
defaults: new { lang = "ru", controller = "Employer", action = "Index" }
);
}
}
So I built a new ASP.NET MVC4 application, some really simple stuff. I added an Area for administration purposes. Everything works fine when I debug on Visual Studio's IIS Express.
The problem comes when I deploy this application on a Windows Server 2012's IIS, most of the time, when I try to acces my Area I get a 404 error. The weired thing is sometimes, after redeploying the application (but not changing anything), the area will work for a certain amount of time (until the application pool gets recycled maybe ?)
Anyway this is really weired and I'd need a hand on this. Here's what I've done :
Here is my RouteConfig for the base application :
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "NewsFilter",
url: "Articles/{tagName}",
defaults: new { controller = "Home", action = "Index", tagName = UrlParameter.Optional },
namespaces: new[] { "myapp.Controllers" }
);
routes.MapRoute(
"ViewArticle",
"Article/{articleId}/{articleTitle}",
new { controller = "Article", action = "Index", articleTitle = UrlParameter.Optional },
new { articleId = #"\d+" },
namespaces: new[] { "myapp.Controllers" }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional },
namespaces: new[] { "myapp.Controllers" }
);
}
}
Now here's my AdminAreaRegistration for the Admin area
public class AdminAreaRegistration : AreaRegistration
{
public override string AreaName
{
get
{
return "Admin";
}
}
public override void RegisterArea(AreaRegistrationContext context)
{
context.MapRoute(
name: "Admin_default",
url: "Admin/Home/{action}/{id}",
defaults: new { controller="Home", action = "Index", id = UrlParameter.Optional },
namespaces: new []{ "myapp.Areas.Admin.Controllers" }
);
context.MapRoute(
name: "Admin_news",
url: "Admin/News/{action}/{id}",
defaults: new { controller="News", action = "Index", id = UrlParameter.Optional },
namespaces: new[] { "myapp.Areas.Admin.Controllers" }
);
context.MapRoute(
name: "Admin_Tags",
url: "Admin/Tags/{action}/{id}",
defaults: new { controller = "Tags", action = "Index", id = UrlParameter.Optional },
namespaces: new[] { "myapp.Areas.Admin.Controllers" }
);
}
}
Now here id the Application_Start method from Global.asax
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas(); //This registers the admin Area
WebApiConfig.Register(GlobalConfiguration.Configuration); //I did not delete WebApiConfig even though I'm not using it
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes); //And here are my "normal" routes
BundleConfig.RegisterBundles(BundleTable.Bundles);
}
All the controllers from my Admin area are in the namespace myapp.Areas.Admin.Controllers. The main app controllers are in the namespace myapp.Controllers.
On IIS, the app pool for the application is running in .net 4.0 Integrated mode.
Any idea why the area is working in debug mode, working sometimes on IIS and returning 404 most of the time ?
I have a simple Controller that is returning thumbnails, it is defined like:
public class ThumbnailsController : ApiController
{
public HttpResponseMessage Get(string id)
{
//code here
}
}
Everything works fine, I can access image using url http://site.com/api/Thumbnails/mylogin
But I would like to modify this method like so:
public class ThumbnailsController : ApiController
{
public HttpResponseMessage Get(string login="", int size=64)
{
//code here
}
}
Idea is to be able to call:
- http://site.com/api/Thumbnails/ - this will return current logged in user picture in default (64x64) size
- http://site.com/api/Thumbnails/mylogin - this will return mylogin user picture in default (64x64) size
- http://site.com/api/Thumbnails/mylogin/128 - this will return mylogin user picture in 128x128 size
My problem are routes, default route works with my unchanged method, but how should I change the default to get this working?
I will also have other Api Controllers but only this one should have custom route.
Here is my attempt, but it isn't working.
routes.MapHttpRoute(
name: "Thumbnails",
routeTemplate: "api/thumbnails/{login}/{size}",
defaults: new {controller="Thumbnails", action="Get", login = RouteParameter.Optional, size = RouteParameter.Optional }
);
EDIT
This is my Controller with test method:
public class ThumbnailsController : ApiController
{
public string Get(string login="", int size=64)
{
return string.Format("login: {0}, size: {1}", login, size);
}
}
and here is my RouteConfig:
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 = "Home", action = "Index", id = UrlParameter.Optional}
);
routes.MapHttpRoute(
name: "Thumbnails",
routeTemplate: "api/Thumbnails/{login}/{size}",
defaults: new { controller = "Thumbnails" , login = RouteParameter.Optional, size = RouteParameter.Optional }
);
}
}
Make sure you declare your custom route before the default route in the WebApiConfig.
EDIT:
public static void Register(HttpConfiguration config)
{
config.Routes.MapHttpRoute(
name: "Thumbnails",
routeTemplate: "api/Thumbnails/{login}/{size}",
defaults: new { controller = "Thumbnails", action = "Get",
login = RouteParameter.Optional, size = RouteParameter.Optional }
);
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
}
The custom route should be before the default one
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapHttpRoute(
name: "Thumbnails",
routeTemplate: "api/Thumbnails/{login}/{size}",
defaults: new { controller = "Thumbnails" , login = RouteParameter.Optional, size = RouteParameter.Optional }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new {controller = "Home", action = "Index", id = UrlParameter.Optional}
);
}
And try to change the type of the "size" to nullable integer:
public HttpResponseMessage Get(string login = null, int? size = 64)