Angular结合VSTO同步Outlook日历遇参数及HTTP请求错误
问题:Angular休假系统通过VSTO同步Outlook日历时参数异常及请求失败
我正在开发一款面向内部开发者的Angular休假管理系统,支持员工申请休假/病假、管理员审批等功能。现在配置通过VSTO向Outlook同步日历数据的功能时遇到问题:点击生成Outlook日历按钮时,请求的year参数为undefined,且HTTP请求返回未知错误。
相关代码
outlook-calendar.service.ts
import { Injectable } from '@angular/core'; import { HttpClient, HttpHeaders } from '@angular/common/http'; import { Observable, BehaviorSubject } from 'rxjs'; import { ReturnHandler } from '../modules/vacation/Models/ReturnHandler'; import { OutlookCalendar } from '../modules/vacation/Models/outlookCalendar'; @Injectable({ providedIn: 'root' }) export class OutlookCalendarService { httpOptions = { headers: new HttpHeaders({ 'Content-Type': 'application/json' }) }; private OutlookUrl = '/api/OutlookCalendar'; private outlookBH$: BehaviorSubject<number> = new BehaviorSubject<number>(0); public outlook$: Observable<number> = this.outlookBH$.asObservable(); constructor(private http: HttpClient) { } AddOutlookAppointment(outlookCalendar: OutlookCalendar) : Observable<ReturnHandler> { const url = `${this.OutlookUrl}/CreateAppointment`; return this.http.post<ReturnHandler>(url, outlookCalendar, this.httpOptions); } DeleteOutlookAppointment(outlookCalendar: OutlookCalendar) : Observable<ReturnHandler> { const url = `${this.OutlookUrl}/DeleteAppointment`; return this.http.post<ReturnHandler>(url, outlookCalendar, this.httpOptions); } OutlookYearCalendarGenerator(year: number) : Observable<ReturnHandler>{ const url = `http://localhost:50304/vac/api/OutlookCalendar/CreateNewOutlookCalendar?year=${year}`; //const url = `${this.OutlookUrl}/CreateNewOutlookCalendar?year=${year}`; console.log(year); return this.http.get<ReturnHandler>(url, this.httpOptions); } // WriteOutlookNationalHolidays(year: string) : Observable<ReturnHandler>{ // const url = `${this.url}/WriteOutlookNationalHolidays?year=${year}`; // return this.http.get<ReturnHandler>(url,this.httpOptions); // } }
ThisAddIn.cs
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Xml.Linq; using Outlook = Microsoft.Office.Interop.Outlook; using Office = Microsoft.Office.Core; using System.Windows.Forms; using System.IO; namespace OutlookCalendar { public partial class ThisAddIn { private OutlookCalendarCs outlookCalendar; protected override object RequestComAddInAutomationService() { if(outlookCalendar == null) outlookCalendar = new OutlookCalendarCs(); return outlookCalendar; } private void ThisAddIn_Startup(object sender, EventArgs e) { Outlook.NameSpace session = Globals.ThisAddIn.Application.Session; Outlook.Accounts accounts = session.Accounts; List<Outlook.MAPIFolder> calendars = new List<Outlook.MAPIFolder>(); foreach (Outlook.Account account in accounts) { Outlook.Recipient recipient = session.CreateRecipient(account.DisplayName); Outlook.MAPIFolder calendar = session.GetSharedDefaultFolder(recipient, Outlook.OlDefaultFolders.olFolderCalendar); if (calendar != null) { calendars.Add(calendar); } } File.WriteAllLines("dd.txt", (IEnumerable<string>)calendars); Console.WriteLine(calendars); using (TextWriter tw = new StreamWriter("C:\\AxessCroTools\\IIS\\dd.txt")) { foreach (Outlook.MAPIFolder calendar in calendars) { tw.WriteLine(calendar); } } } private void ThisAddIn_Shutdown(object sender, System.EventArgs e) { // Note: Outlook no longer raises this event. If you have code that // must run when Outlook shuts down, see https://go.microsoft.com/fwlink/?LinkId=506785 } #region VSTO generated code /// <summary> /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// </summary> private void InternalStartup() { this.Startup += new System.EventHandler(ThisAddIn_Startup); this.Shutdown += new System.EventHandler(ThisAddIn_Shutdown); } #endregion } }
错误信息
Http failure response for http://localhost:50304/vac/api/OutlookCalendar/CreateNewOutlookCalendar?year=undefined: 0 Unknown Error
排查方向
参数传递问题:
- 检查调用
OutlookYearCalendarGenerator方法的组件代码,确认传入的year值是否正确初始化或获取(比如从日期选择器、表单输入等来源),避免出现未定义的情况。 - 查看浏览器控制台输出的
year值(代码中已添加console.log(year)),确认调用方法时参数是否确实未传递。
- 检查调用
API请求配置问题:
- 当前硬编码了API地址,建议改用代码中注释的相对路径
${this.OutlookUrl}/CreateNewOutlookCalendar?year=${year},避免跨域或环境不一致问题。 - 确认后端API服务是否正常运行,
CreateNewOutlookCalendar接口能否正常接收year参数并返回有效响应。
- 当前硬编码了API地址,建议改用代码中注释的相对路径
VSTO侧关联问题:
- 当前VSTO代码仅在启动时获取日历列表并写入文件,未实现与Angular前端的交互逻辑。需确认后端API是否正确调用了VSTO的自动化服务(
OutlookCalendarCs类)来生成日历,同时确保VSTO插件已正确加载并注册。
- 当前VSTO代码仅在启动时获取日历列表并写入文件,未实现与Angular前端的交互逻辑。需确认后端API是否正确调用了VSTO的自动化服务(
内容的提问来源于stack exchange,提问作者Hrvoje
相关产品推荐
相关产品推荐

