MVC 6 (ASP.NET Core 1.0) is slightly different from the way filters are registered:
Startup.cs - AddMvc with filter for RequireHttpsAttribute :
public void ConfigureServices(IServiceCollection services) {
Design solutions are discussed:
- Use a filter in Startup.cs for global configuration (since we want this to apply everywhere). The launch should be responsible for registering and configuring all global rules. If a new developer will work in your company, it expects to find a global setting in Startup.cs.
- Use the RequireHttpsAttribute logic as proven (Microsoft). Never use βmagicβ lines such as βhttp: //β and βhttps: //β when this can be avoided by reusing the Microsoft component created to provide the same logic.
If you use your MVC website in a local hosting without SSL:
- http : // localhost: 1337 / (without SSL)
- https : // localhost: 1337 / (SSL)
Consider starting without SSL on the local host, while requiring an https file .
Note:
As an alternative , we can create a "BaseController: Controller class" and make all our controllers inherit from the "BaseController" (instead of Controller). Then we only need to set the attribute 1 global location (and do not need to register the filter in Startup.cs).
Some people prefer attribute style.
Usage example:
[RequireHttpsAttribute] public class BaseController : Controller {
Nick Niebling Jul 07 '16 at 12:25 2016-07-07 12:25
source share