LoginSignup
1

More than 5 years have passed since last update.

XBAPとASP.NET MVCをどうやって連携するか

Posted at

このドキュメントではASP.NET MVCとXBAPをどうやって連携するか考える。

XBAPのファイルをどう配置すべきか?

XBAPは別プロジェクトにする。
ASP.NET MVCのプロジェクトはXBAPのプロジェクトに依存させる。
xbap001.png

ASP.NET MVCのxabapフォルダを対象にxbapを発行するようにしとけばいい。
VisualStudioからXBAPプロジェクトを選んで一々発行するのもいいが、以下のようにASP.NET MVCプロジェクトのビルド前イベントにバッチを組むといい。

1.MSBUILD用のスクリプトを記述する。

<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" DefaultTargets="Publish" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
    <PropertyGroup>
        <!-- the folder of the project to build -->
        <ProjLocation>.\WpfBrowserApplication1</ProjLocation>
        <ProjLocationReleaseDir>$(ProjLocation)\bin\Release</ProjLocationReleaseDir>
        <ProjPublishLocation>$(ProjLocationReleaseDir)\app.publish</ProjPublishLocation>
        <!-- This is the web-folder, which provides the artefacts for click-once. After this
         build the project is actually deployed on the server -->
        <DeploymentFolder>MvcApplicationTest\xbap</DeploymentFolder>
    </PropertyGroup>


    <Target Name="Publish" DependsOnTargets="Clean">
        <Message Text="Publish-Build started for build no $(ApplicationRevision)" />
        <MSBuild Projects="$(ProjLocation)/WpfBrowserApplication1.csproj" Properties="Configuration=Release" Targets="Publish"/>   


        <ItemGroup>
            <SchoolPlannerSetupFiles Include="$(ProjPublishLocation)\*.*"/>
            <SchoolPlannerUpdateFiles Include="$(ProjPublishLocation)\Application Files\**\*.*"/>
        </ItemGroup>
        <Copy
            SourceFiles="@(SchoolPlannerSetupFiles)"
            DestinationFolder="$(DeploymentFolder)\"
        />
        <Copy
            SourceFiles="@(SchoolPlannerUpdateFiles)"
            DestinationFolder="$(DeploymentFolder)\Application Files\%(RecursiveDir)"
        />      
    </Target>
    <Target Name="Clean">   
        <Message Text="Clean project:" />
        <MSBuild Projects="$(ProjLocation)/WpfBrowserApplication1.csproj" Properties="Configuration=Release" Targets="Clean"/>
    </Target>       
</Project>

2.ASP.NET MVCプロジェクトのビルド前イベントで以下のようなバッチを記述する

Mage.exe -cc
"C:\Program Files (x86)\MSBuild\12.0\Bin\MSBuild.exe" "$(SolutionDir)deployxbap.xml" /target:publish

Marge.exeはXBAPのキャッシュをクリアしている。

ASP.NET MVCからどうXBAPにデータを渡すべきか?

セッションは無理。
クエリーストリングを使うか、ブラウザーのクッキーつかうか、どっちか。

クエリーストリングを使う方法

1.XBAPプロジェクトのプロパティで「発行」のオプションを押す
xbap002.png

2.マニフェストで「URLパラメータをアプリケーションに渡すことを許可する」
xbap003.png

3.XBAPプロジェクトに「System.Deployment」を参照設定する。

4.ASP.NET MVCプロジェクトからXBAPのパスを呼ぶときにパラメータを与える。

    <iframe height="130"
        width="100%"
        src="/xbap/WpfBrowserApplication1.xbap?param=1" />

5.XBAP側の実装で以下のようにするとクエリー付きのURLがとれるので解析しとく。

Uri launchUri = System.Deployment.Application.ApplicationDeployment.CurrentDeployment.ActivationUri;

セッションを使う方法

1.ASP.NET MVCプロジェクトのビューで以下のような実装をする。ASPXの例だが、razorとかでもできるはず。

    <%
        // Construct the cookies.
HttpCookie cookie = new HttpCookie("MyData");
cookie.Value = HttpUtility.HtmlEncode("MyData");

//Add to the response stream
Response.Cookies.Add(cookie);
    %>

2.XBAPプロジェクト側は次のような実装で、クッキーの中身をとる

            string cookieString = "";
            try
            {
                String cookie = Application.GetCookie(System.Windows.Interop.BrowserInteropHelper.Source);
                cookieString = cookie;
            }
            catch (Exception)
            {
            }

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1