How to use Vue with ASP.NET Razor
TLDR
- Architectural Integration: Initialize the Vue instance globally via
_Layout.cshtmland usemixinsto inject page-specific logic for modular management. - Preventing FOUC: Use
v-cloakcombined with CSS[v-cloak] { display: none; }to resolve template flashing during page load. - Avoiding Conflicts: Do not use
<script>tags within Vue templates; use@@to escape Razor syntax when it conflicts with Vue syntax (e.g.,@). - Ajax Security: Use
axiosinterceptors to automatically add theRequestVerificationTokento headers to comply with ASP.NET Core anti-forgery requirements. - Validation Mechanism: Use custom Tag Helpers to convert C# Model Validation attributes into
VeeValidaterules, enabling real-time frontend feedback while maintaining backend validation.
Architectural Integration: Collaboration between Vue and Razor
When integrating Vue into ASP.NET Razor Pages, it is recommended to centralize the initialization logic of the Vue instance in _Layout.cshtml and use the mixins mechanism to inject data and methods for each page.
Core Implementation
- Root Node Setup: Define a container with
v-cloakin_Layout.cshtmlto ensure content remains hidden until Vue compilation is complete. - Mixin Injection: Declare a global
mixinsarray in_Layout.cshtml, and have each pagepushits specific logic into this array via the@section Scriptsblock. - Initialization: Finally, perform
new Vue({ el: '#vueApp', mixins: mixins })at the bottom of_Layout.cshtml.
Common Development Notes
1. Avoiding Template Compilation Errors
When this occurs: Writing <script> tags directly within the Vue mounting scope.
- Reason: Vue templates are only responsible for UI mapping and do not allow tags with side effects.
- Recommendation: Move all JavaScript logic to the
@section Scriptsblock.
2. Razor and Vue Syntax Conflicts
When this occurs: Using Vue's @ shorthand syntax (e.g., @click) in a Razor page.
- Reason:
@is a reserved keyword in Razor, which will cause compilation errors. - Recommendation: Use
@@to escape it (e.g.,@@click). If used within a Tag Helper, ensure the@only appears within attribute values; otherwise, the Tag Helper may fail to parse.
Alternatives to jQuery
Ajax Requests and XSRF Protection
Since ASP.NET Core Razor Pages automatically perform anti-forgery validation, you must ensure that axios includes the correct token in the header.
axios.interceptors.request.use(
config => {
let token = document.querySelector('input[name="__RequestVerificationToken"]');
if (token !== null) {
config.headers = {
RequestVerificationToken: token.value
}
}
return config;
},
error => Promise.reject(error)
);Frontend Validation: Integrating VeeValidate with Model Validation
When this occurs: You want to implement real-time frontend validation without abandoning native ASP.NET validation attributes like [Required] or [EmailAddress].
- Solution: Write a custom
TagHelperto convert C#ModelMetadataintoVeeValidateHTML attributes (e.g.,v-validate). - Validation Results:
- Automatically generate validation rules via
VeeValidationInputTagHelper. - Bind
v-showand error message displays viaVeeValidationMessageTagHelper. - In the Vue
createdhook, inject backendModelStateerror messages intothis.$validator.errorsto ensure the frontend displays errors synchronously when backend validation fails.
- Automatically generate validation rules via
Change Log
- 2022-10-24 Initial documentation created.