I add Volo.Account module with source code to my solution for update some functionality of Login/Register. When I update page (like Login.cshtml) changes not shown until restart project.
According to Microsoft doc, I instal Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation package and line below to ConfigureServices of {PROJECT}AuthServerModule, but RuntimeCompilation not working.
context.Services.AddRazorPages()
.AddRazorRuntimeCompilation();
actually you don't need to use AddRazorRuntimeCompilation in your application. You can get the advantage of ABP's Virtual File System and configure the AbpVirtualFileSystemOptions in your application module class:
public override void ConfigureServices(ServiceConfigurationContext context)
{
var hostingEnvironment = context.Services.GetHostingEnvironment();
var configuration = context.Services.GetConfiguration();
//other configurations...
if (hostingEnvironment.IsDevelopment())
{
Configure<AbpVirtualFileSystemOptions>(options =>
{
options.FileSets.ReplaceEmbeddedByPhysical<AbpAccountWebModule>(Path.Combine(hostingEnvironment.ContentRootPath, string.Format("<web-module-project-path>")));
});
}
}
You just need to use the ReplaceEmbeddedByPhysical method. Check the following links for more info:
https://docs.abp.io/en/abp/latest/Virtual-File-System#dealing-with-embedded-files-during-development
https://github.com/abpframework/abp/blob/dev/templates/app/aspnet-core/src/MyCompanyName.MyProjectName.Web/MyProjectNameWebModule.cs#L148
Related
I'm getting an error when I'm attempting to run my page says that,
The name 'ConfigureAuth' does not exist in the current context
in my Stratup Class. I'm sure all AspNet Identity libraries are installed. What do I need to do next, to try to fix this?
using Microsoft.Owin;
using Owin;
[assembly: OwinStartupAttribute(typeof(project_name.Startup))]
namespace project_name
{
public partial class Startup
{
public void Configuration(IAppBuilder app)
{
ConfigureAuth(app);
}
}
}
If you are using default Visual Studio project template, the ConfigureAuth method could be found in partial class Startup.Auth.cs. So make sure you didn't break anything when modifying project structure.
This is an example of ConfigureAuth method:
// For more information on configuring authentication, please visit http://go.microsoft.com/fwlink/?LinkId=301864
public void ConfigureAuth(IAppBuilder app)
{
// Configure the db context and user manager to use a single instance per request
app.CreatePerOwinContext<ApplicationUserManager>(ApplicationUserManager.Create);
// Enable the application to use a cookie to store information for the signed in user
// and to use a cookie to temporarily store information about a user logging in with a third party login provider
app.UseCookieAuthentication(new CookieAuthenticationOptions());
app.UseExternalSignInCookie(DefaultAuthenticationTypes.ExternalCookie);
// Configure the application for OAuth based flow
PublicClientId = "self";
OAuthOptions = new OAuthAuthorizationServerOptions
{
TokenEndpointPath = new PathString("/api/Token"),
Provider = new ApplicationOAuthProvider(PublicClientId),
AuthorizeEndpointPath = new PathString("/api/Account/ExternalLogin"),
AccessTokenExpireTimeSpan = TimeSpan.FromDays(14),
AllowInsecureHttp = true
};
// Enable the application to use bearer tokens to authenticate users
app.UseOAuthBearerTokens(OAuthOptions);
}
I had similar issue, To fix the issue I removed .App_Start from namespace in Startup.Auth.cs file. After that I was able to see the reference.
It is either:
[assembly: **OwinStartup**(typeof(Project-Name.Startup))]
namespace project-name
{
public partial class Startup
{
public void **Configuration**(IAppBuilder app)
{
OR
[assembly: **OwinStartupAttribute**(typeof(Project-Name.Startup))]
namespace project-name
{
public partial class Startup
{
public void **ConfigureAuth**(IAppBuilder app)
{
Either rename OwinStartupAttribute to OwinStartup
OR Configuration to ConfigureAuth
Kindly I note that the two partial classes (Startup.Auth.cs and Startup.cs) should be in the same namespace which is the root of the project, just change the namespace of Startup.Auth.cs to the same namespace of the Startup.cs
Make sure when you originally create the project that there are no spaces in the name.
e.g. my app was called "DevOps Test" which was giving me errors when I ran it.
I recreated it as "DevopsTest" and no longer had these issues
namespace PAYOnline.App_Start
delete App_Start only namespace PAYOnline => It's welldone
I am new to ASP.NET core and I am trying to build a small web app but whenver I make a change to a view in debugging mode, I have to restart the application to see the new changes.
I tried few tricks but they did not work and even decorating a specific view with the filter
[ResponseCache(Location = ResponseCacheLocation.None, NoStore = true)]
First, Install Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation NuGet package.
Second, Update the project's Startup.ConfigureServices method to include a call to AddRazorRuntimeCompilation.
public void ConfigureServices(IServiceCollection services)
{
services.AddRazorPages()
.AddRazorRuntimeCompilation();
// code omitted for brevity
}
Then you can update the page without restart the project.
I am trying to restrict user to click back button after Login/Logout into the application.
If the user is logged in into the application, then after clicking on back button Login view should not be displayed and if user is logged out from the application then clicking on back button LogOff view should not be displayed.
I have already used caching technique which is given in number of links, but seems its not working.
This CacheAfterLogout link and done exactly as it is, still problem is not solved. I am using asp.net identity framework.
Could someone help on this ?
To disable caching you can apply the following to each controller you wish to disable caching on
[ResponseCache(Location = ResponseCacheLocation.None, NoStore = true)]
You can also setup named cache profiles and configure the settings at runtime.
Cache can be disabled by applying the [ResponseCache(Location = ResponseCacheLocation.None, NoStore = true)] on each controller for which you wish to disable caching.
This can be applied to all controllers globally by adding your custom filter in startup.cs while configuring MVC services with ASP.Net Core 1.0.-* and MVC6.
First create a custom cache filter implementing the ResponseCacheAttribute as follows
public class ResponseCacheFilter : ResponseCacheAttribute
{
public ResponseCacheFilter() : base()
{
Location = ResponseCacheLocation.None;
NoStore = true;
}
}
This should be added in startup.cs file as follows.
public void ConfigureServices(IServiceCollection services)
{
// Add framework and custom services.
services.AddMvc(config =>
{
config.Filters.Add(new ResponseCacheFilter());
});
}
See the configuration of ResponseCache at https://docs.asp.net/en/latest/performance/caching/response.html#responsecache-attribute
Just Add This Code in Global.asax.cs
protected void Application_BeginRequest()
{
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.Cache.SetExpires(DateTime.UtcNow.AddHours(-1));
Response.Cache.SetNoStore();
}
Just add these lines to your Global.asax.cs file.
protected void Application_BeginRequest()
{
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.Cache.SetExpires(DateTime.UtcNow.AddHours(-1));
Response.Cache.SetNoStore();
}
How can we get compile time errors in views and view components in Mvc6?
In MVC 5, it was simple to edit the .csproj file. How do we do it now?
Answer for RC-1
Create the following RazorPreCompilation class:
namespace YourApp.Compiler.Preprocess
{
public sealed class RazorPreCompilation : RazorPreCompileModule
{
protected override bool EnablePreCompilation(BeforeCompileContext context) => true;
}
}
Places this class in {root}/Compiler/Preprocess.
Add a call to AddPrecompiledRazorViews in your ConfigureServices method.
public void ConfigureServices(IServiceCollection services)
{
// ... other code ...
services
.AddMvc()
.AddPrecompiledRazorViews(GetType().GetTypeInfo().Assembly);
// ... other code ...
}
As noted in other answers, when views are precompiled you lose the ability to change them while the application is running. Additionally, it seems that sometimes you have to do a rebuild on your project (as opposed to just a build) in order for new code changes to take effect. For example, correcting a typo in a Razor method call may still trigger a compilation error until you rebuild the project.
Update on Directives
If you'd like to have this run with preprocess directives, wrap them around the AddPrecompiledRazorViews call like this:
public void ConfigureServices(IServiceCollection services)
{
// ... other code ...
var mvcBuilder = services.AddMvc()
#if !DEBUG
mvcBuilder.AddPrecompiledRazorViews(GetType().GetTypeInfo().Assembly);
#endif
// ... other code ...
}
You can use pre-compiled views by adding the following class to your project. However, doing this stops you from being able to edit the views at runtime. Therefore, I have added the #if !DEBUG pre-processor directive so that the views are only pre-compiled in release mode.
#if !DEBUG
namespace MvcBoilerplate.Compiler.Preprocess
{
using System;
using Microsoft.AspNet.Mvc;
/// <summary>
/// Enable pre-compilation of Razor views, so that errors in your .cshtml files are caught and displayed
/// in the Visual Studio errors window at compile time, rather than your sites users receiving a runtime 500
/// internal server error. Pre-compilation may reduce the time it takes to build and launch your project but will
/// cause the build time to increase. It will also stop edit and continue working for .cshtml files.
/// </summary>
public class RazorPreCompilation : RazorPreCompileModule
{
}
}
#endif
Secondly, in Startup.cs you need to actually use the pre-compiled views which you can do like so:
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc().AddPrecompiledRazorViews(GetType().GetTypeInfo().Assembly);
}
Pre-compiled views give quicker application startup performance.
You must add a class
public class RazorPreCompilation : RazorPreCompileModule
{
public RazorPreCompilation(IServiceProvider provider) : base(provider)
{
this.GenerateSymbols = true;
}
}
and Add AddPrecompiledRazorViews to your mvc setup in your startup class
see this question
Try:
I created a new project in VS2012
I installed via the NuGet package RavenDB Embedded -Pre
I installed Ninject.MVC3
Added a module for ninject RavenDB:
Public class RavenDBNinjectModule : NinjectModule
{
public override void Load()
{
Bind<IDocumentStore>().ToMethod(context =>
{
NonAdminHttp.EnsureCanListenToWhenInNonAdminContext(8080);
var documentStore = new EmbeddableDocumentStore { Url="http://localhost:8080/", DataDirectory="~/App_Data", UseEmbeddedHttpServer = true };
return documentStore.Initialize();
}).InSingletonScope();
Bind<IDocumentSession>().ToMethod(context => context.Kernel.Get<IDocumentStore>().OpenSession()).InRequestScope();
}
}
In my class "NinjectWebCommon" ...
private static void RegisterServices(IKernel kernel)
{
kernel.Load(new RavenDBNinjectModule());
}
When running the application, the following url was generated ("http://localhost:1423")
Verify that the file "Raven.Studio.xap" was the root of my application
I tried accessing "http://localhost:8080" but the following screen is displayed:
What am I doing wrong?
As it turned out, the issue is that documentStore.Initialize never get called, because that no one did ask Ninject to resolve IDocumentStore.
You are setting the Url property, which means that you aren't running in embedded mode, but in server mode.
Remove the Url property, and everything will work for you.
I found the problem!
Since he had used IDocumentSession in no time, the ninject had not created the instance of IDocumentStore and thus not run the Initialize method