Browse Source

Added handling of all missing routes with custom 404 page.

tags/2.0.6
Teknikode 3 years ago
parent
commit
c98e6cbf8e

+ 10
- 0
Teknik/App_Start/RouteConfig.cs View File

@@ -8,6 +8,16 @@ namespace Teknik
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

// Add this code to handle non-existing urls
routes.MapRoute(
name: "404-PageNotFound",
// This will handle any non-existing urls
url: "{*url}",
// "Shared" is the name of your error controller, and "Error" is the action/page
// that handles all your custom errors
defaults: new { controller = "Default", action = "NotFound" }
);
}
}
}

+ 15
- 0
Teknik/App_Start/SubdomainRouteExtension.cs View File

@@ -91,6 +91,21 @@ namespace Teknik
return route;
}

public static SubdomainRoute MapSubdomainRoute(this AreaRegistrationContext context, string name, List<string> subDomains, List<string> domains, string url, object defaults, object constraints, string[] namespaces)
{
SubdomainRoute route = new SubdomainRoute(
subDomains,
domains,
url,
new RouteValueDictionary(defaults),
new RouteValueDictionary(constraints),
new RouteValueDictionary(new { Area = context.AreaName, Namespaces = namespaces }),
new MvcRouteHandler());

context.Routes.Add(name, route);
return route;
}

public static SubdomainRoute MapSubdomainRoute(this AreaRegistrationContext context, string name, List<string> subDomains, List<string> domains, string url, string area, object defaults)
{
SubdomainRoute route = new SubdomainRoute(

+ 11
- 0
Teknik/Areas/Home/HomeAreaRegistration.cs View File

@@ -42,6 +42,17 @@ namespace Teknik.Areas.Home
new[] { typeof(DefaultController).Namespace }
);

// Register fallback for all bad requests
context.MapSubdomainRoute(
"Default.NotFound", // Route name
new List<string>() { "*" }, // Subdomains
new List<string>() { config.Host }, // domains
"{url}", // URL with parameters
new { controller = "Default", action = "NotFound" }, // Parameter defaults
new { url = "{*url}" },
new[] { typeof(DefaultController).Namespace }
);

context.MapSubdomainRoute(
"Home.Index", // Route name
new List<string>() { "www", string.Empty }, // Subdomains

+ 1
- 1
Teknik/Areas/Home/Views/Home/Index.cshtml View File

@@ -29,7 +29,7 @@
<br />
<div class="row text-center">
<h2>
Teknik is dedicated to the advancement of technology and ideas, and we provide these services to help those who try to innovate.
We provide these services to help those who try to innovate.
</h2>
</div>
</div>

+ 12
- 0
Teknik/Controllers/DefaultController.cs View File

@@ -65,6 +65,18 @@ namespace Teknik.Controllers
string imageFile = Server.MapPath(Constants.LOGO_PATH);
return File(imageFile, "image/svg+xml");
}

[HttpGet]
[AllowAnonymous]
public ActionResult NotFound()
{
var errorController = new ErrorController();
if (errorController != null)
{
return errorController.Http404(new Exception("Page Not Found"));
}
return null;
}
}

[AttributeUsage(AttributeTargets.Method, AllowMultiple = false)]

+ 2
- 2
Teknik/Web.config View File

@@ -37,11 +37,11 @@
<forms domain=".teknik.io" protection="All" enableCrossAppRedirects="true" name="TeknikAuth" />
</authentication>
<compilation debug="true" targetFramework="4.6.2" />
<httpRuntime targetFramework="4.5.2" maxRequestLength="1048576" executionTimeout="3600" />
<httpRuntime targetFramework="4.5.2" maxRequestLength="1048576" executionTimeout="3600" relaxedUrlToFileSystemMapping="true" />
<pages buffer="true" enableViewState="false" />
</system.web>
<system.webServer>
<modules>
<modules runAllManagedModulesForAllRequests="true">
<remove name="FormsAuthentication" />
<add name="FormsAuthentication" type="System.Web.Security.FormsAuthenticationModule" />
<add name="PerfModule" type="Teknik.Modules.PerformanceMonitorModule, Teknik" />

Loading…
Cancel
Save