MVC经典实例
来源:互联网 发布:英镑符号 mac系统 编辑:程序博客网 时间:2024/05/17 03:59
http://www.csdn.net/article/2015-08-28/2825567
在门户上我们可以建立和管理云服务,我们本章将用SDK来建立和管理云服务。
非常有意思的是,SDK中云服务管理的客户端不是CloudServiceManagementClient而是ComputeManagementClient。云服务的开发需要从你的订阅部署文件从提取订阅ID、订阅密钥。我们将这些值在Web.config中存储。
<add key="SubscriptionCertificate"value=""/>
<add key="SubscriptionID"value=""/>
<add key="ServiceManagementUrl"value="https://management.core.chinacloudapi.cn"/>
我们需要从NuGet获取。
Microsoft.WindowsAzure.Management
Microsoft.WindowsAzure.Management.Compute
Microsoft.WindowsAzure.Management.Libraries
引用完成后,我们建立本章的控制器:HostedServicesController,该控制器有如下Action。
- Index
- Create
- Delete
- Details
[Authorize]
public class HostedServicesController : Controller
{
privatereadonly string SubscriptionCertificate =CloudConfigurationManager.GetSetting("SubscriptionCertificate");
privatereadonly string SubscriptionID =CloudConfigurationManager.GetSetting("SubscriptionID");
private readonlystring ServiceManagementUrl =CloudConfigurationManager.GetSetting("ServiceManagementUrl");
privatereadonly Microsoft.WindowsAzure.Management.Compute.ComputeManagementClientclient = null;
publicHostedServicesController()
{
var certificateData= Convert.FromBase64String(SubscriptionCertificate);
varcryptography = newSystem.Security.Cryptography.X509Certificates.X509Certificate2(certificateData);
varcertificateCloudCredentials = new Microsoft.WindowsAzure.CertificateCloudCredentials(SubscriptionID,cryptography);
client =newMicrosoft.WindowsAzure.Management.Compute.ComputeManagementClient(certificateCloudCredentials,new Uri(ServiceManagementUrl));
}
}
在构造函数中,我们必须实例化证书然后通过该证书和ServiceManagementUrl实例化ComputeManagementClient,记住ServiceManagementUrl是必须的,且如果你用21世纪服务,该Url必须是https://management.core.chinacloudapi.cn
一、Index列出云服务
代码不难,但是记得云的方式都是异步操作。
public async Task<ActionResult> Index()
{
var list =await client.HostedServices.ListAsync(newSystem.Threading.CancellationTokenSource().Token);
list.ToList();
returnView(list);
}
对应的View
@using System.Net
@model Microsoft.WindowsAzure.Management.Compute.Models.HostedServiceListResponse
@{
ViewBag.Title ="Index";
}
<h2>HostedServices Index</h2>
<p>
@Html.ActionLink("Create New", "Create")
</p>
<div class="table-responsive">
<tableid="directoryObjects" class="table table-bordered table-stripedtable-condensed">
<tr>
<th>
云服务名称
</th>
<th>
位置
</th>
<th>
状态
</th>
<th>
创建时间
</th>
<th>
更新时间
</th>
<th/>
</tr>
@foreach(var item in Model)
{
<tr>
<td>
@Html.ActionLink(item.ServiceName, "Details", new {serviceName = item.ServiceName })
</td>
<td>
@Html.Label(item.Properties.Location)
</td>
<td>
@Html.Label(item.Properties.Status.ToString())
</td>
<td>
@Html.Label(item.Properties.DateCreated.ToString())
</td>
<td>
@Html.Label(item.Properties.DateLastModified.ToString())
</td>
<td>
@Html.ActionLink("Delete", "Delete", new {serviceName = item.ServiceName }) <br/>
</td>
</tr>
}
</table>
</div>
如果你使用21世纪服务,Location的值为ChinaEast或China North。运行的结果如下。
二、Create创建云服务
创建云服务将用到一个参数类HostedServiceCreateParameters,这个类描述了云服务创建用的信息,不过目前常用的参数就是Location。Location的值只能在China East或China North之间。
[HttpPost]
public async Task<ActionResult> Create(FormCollection values)
{
var hostServicesPar = new Microsoft.WindowsAzure.Management.Compute.Models.HostedServiceCreateParameters(values["serviceName"], values["serviceName"])
{
Location = values["location"]
};
await client.HostedServices.CreateAsync(hostServicesPar, new System.Threading.CancellationTokenSource().Token);
return RedirectToAction("Index");
}
对应的View为
@model Microsoft.WindowsAzure.Management.Compute.Models.HostedServiceListResponse.HostedService
@{
ViewBag.Title = "Create HostedService";
}
<h2>Create HostedService</h2>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>HostedService</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
<p>设置服务名称</p>
@Html.TextBox("serviceName")
</div>
<div class="form-group">
<p>设置服务位置。</p>
@Html.DropDownList("location", new List<SelectListItem>() { new SelectListItem() { Value = "China East", Text = "中国东部" }, new SelectListItem() { Value = "China North", Text = "中国北部" } })
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="创建" class="btn btn-default" />
</div>
</div>
</div>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}
该View中我用了Html.DropDownList让用户在China East或China North中进行选择。运行后用户可以创建新的云服务,并选择位置
在门户上我们可以建立和管理云服务,我们本章将用SDK来建立和管理云服务。
同时你也可以访问Azure门户确认该云服务的确是被创建了
三、Details获取云服务的详细信息
云服务的信息主要可以通过Properties和ExtendedProperties获取。
public async Task<ActionResult> Details(string serviceName)
{
var hostservice = await client.HostedServices.GetAsync(serviceName, new System.Threading.CancellationTokenSource().Token);
return View(hostservice);
}
对应的View为
@model Microsoft.WindowsAzure.Management.Compute.Models.HostedServiceGetResponse
@{
ViewBag.Title = "Details";
}
<h2>Details</h2>
<div>
<h4>@ViewBag.Path</h4>
<hr />
<dl class="dl-horizontal">
<dt>
服务名称
</dt>
<dd>
@Html.Label(Model.ServiceName)
</dd>
<dt>
Url
</dt>
<dd>
@Html.Label(Model.Uri.ToString())
</dd>
<dt>
RequestId
</dt>
<dd>
@Html.Label(Model.RequestId)
</dd>
<dt>
位置
</dt>
<dd>
@Html.Label(Model.Properties.Location)
</dd>
<dt>
DNS
</dt>
<dd>
@Html.Label(Model.Properties.ReverseDnsFqdn == null ? "" : Model.Properties.ReverseDnsFqdn)
</dd>
<dt>
状态
</dt>
<dd>
@Html.Label(Model.Properties.Status.ToString())
</dd>
<dt>
创建日期【更新日期】
</dt>
<dd>
@Html.Label(string.Format("{0}【{1}】", Model.Properties.DateCreated, Model.Properties.DateLastModified))
</dd>
@foreach (var expro in Model.Properties.ExtendedProperties)
{
<dt>
@Html.Label(expro.Key)
</dt>
<dd>
@Html.Label(expro.Value)
</dd>
}
</dl>
</div>
<p>
@Html.ActionLink("Back to List", "Index")
</p>
运行后如下图。
四、Delete删除云服务
删除云通过服务名称进行查找服务并删除。
public async Task<ActionResult> Delete(string serviceName)
{
await client.HostedServices.DeleteAsync(serviceName, new System.Threading.CancellationTokenSource().Token);
return RedirectToAction("Index");
}
- MVC经典实例
- MVC 模式(很经典的时钟实例)
- MVC入门——经典MVC登录实例详解--附完整代码
- 经典实例
- MVC日积月累 经典学习
- MVC经典框架结构图
- 经典软件设计模型 - MVC
- spring mvc 经典讲解
- 经典Spring mvc 文章
- 经典软件设计模型 - MVC
- 经典软件设计模型 - MVC
- mvc的一个实例
- MVC架构实例
- Extjs4.0 MVC实例
- spring mvc 简单实例
- MVC 路由扩展 实例
- Mvc项目实例 MvcMusicStore
- Spring MVC-实例1
- Android 事件分发机制完全解析(续)
- Student Information System
- 黑马程序员———C语言 函数
- 光照模型的计算方式总结(1.Diffuse分量2.Specular分量 Phong model和Blinn-Phong model (Or Blinn))
- 稀疏矩阵的三元组表示的实现及应用(一)
- MVC经典实例
- UITableView重要点整理
- 第9周项目3-稀疏矩阵的三元组表示的实现及应用(1)
- php文件操作
- AIDL初探(本地、进程)
- 2015-11-2 【项目3 - 利用二叉树遍历思想解决问题】
- 第十周项目1-“知原理”检验题目
- 第十周实践项目1—二叉树算法库
- 怎样理解阻塞非阻塞与同步异步的区别?