asp.net 关闭父窗体_引入ASP.NET FriendlyUrls-更清晰的URL,更轻松的路由和ASP.NET Web窗体的移动视图

asp.net 关闭父窗体

asp.net 关闭父窗体

I've said before how surprised I am that more ASP.NET Web Forms developers don't use Routing to make their URLs prettier. If you don't want "foo.aspx" in your URL, then change it with Routes.MapPageRoute(). However, managing Routing Tables is a little tedious and most WebForms folks aren't used to the concept and don't want to invest the time.

我之前曾说过,令我感到惊讶的是,更多的ASP.NET Web窗体开发人员没有使用“路由”来使自己的URL更漂亮。 如果您不想在URL中使用“ foo.aspx”,请使用Routes.MapPageRoute()进行更改。 但是,管理路由表有点乏味,并且大多数WebForms人士都不习惯这种概念,也不想浪费时间。

I've also heard a number of ASP.NET Web Forms Developers express a little envy at how easy it is to make a site that has both desktop and mobile views using ASP.NET MVC. They like the idea of seeing an iPhone show up and showing a different  view while reusing logic as I've shown in my mobile talks before.

我还听说许多ASP.NET Web窗体开发人员对使用ASP.NET MVC制作同时具有桌面视图和移动视图的网站有多么容易表示嫉妒。 他们喜欢这样的想法:看到iPhone出现并显示不同的视图,同时重用我之前在移动演讲中展示的逻辑。

Let's solve both these problems with a new ASP.NET feature just pre-released today in alpha form on NuGet. My peer Damian Edwards and developer Levi Broderick along with QA by  Pranav and Anton have come up with a pretty awesome solution based on the original "Smarty Routes" idea from Eilon Lipton and the result is FriendlyUrls.

让我们用今天刚在NuGet上以alpha形式预发布的新ASP.NET功能解决这两个问题。 我的同伴Damian Edwards和开发人员Levi Broderick以及Pranav和Anton的QA提出了一个非常棒的解决方案,该解决方案基于Eilon Lipton最初提出的“ Smarty Routes”构想,其结果是FriendlyUrls

Install-Package Microsoft.AspNet.FriendlyUrls -pre

NOTE: If you've been paying attention to ASP.NET for the last few months you'll recognize this incremental useful but appropriately sized forward motion as being all part of the One ASP.NET master plan.

注意:如果最近几个月一直在关注ASP.NET,您将认识到此增量有用但大小合适的前进动作是One ASP.NET总体规划的一部分

It's also worth noting that this FriendlyUrls NuGet package includes BOTH an ASP.NET 4.5 and ASP.NET 4 version so .NET 4 folks get love too.

还值得注意的是,这个FriendlyUrls NuGet软件包同时包含ASP.NET 4.5和ASP.NET 4版本,因此.NET 4的人们也能从中获得爱。

FriendlyUrls Hello World示例 (FriendlyUrls Hello World Example)

First, the obvious example. Bring up Visual Studio and File | New Project | New ASP.NET Web Forms Application. Now, from the Package Manager Console or from Manage NuGet Packages, install Microsoft.AspNet.FriendlyUrls. You'll need to "Include Prerelease" packages with -pre from the command line or via the dropdown in the UI.

首先,一个明显的例子。 调出Visual Studio和文件| 新项目| 新的ASP.NET Web窗体应用程序。 现在,从程序包管理器控制台或从“管理NuGet程序包”,安装Microsoft.AspNet.FriendlyUrls。 您需要从命令行或通过用户界面中的下拉菜单使用-pre“包括Prerelease”软件包。

Microsoft.AspNet.FriendlyUrls -pre shown in the UI

Be sure to read the readme.txt that pops up as you'll need to ensure that the FriendlyUrls routing gets called on application startup! I added this one line to my Application_Start:

请确保阅读弹出的readme.txt,因为您需要确保在应用程序启动时调用FriendlyUrls路由! 我将这一行添加到我的Application_Start中:

RouteConfig.RegisterRoutes(RouteTable.Routes);

Here's the cool part. If I hit one of my existing links, like Contact.aspx, look what happened. See how the GET request for /Contact.aspx turned into a 301 redirect to /Contact?

这是很酷的部分。 如果我点击了我现有的链接之一,例如Contact.aspx,请看看发生了什么。 看到对/Contact.aspx的GET请求如何将301重定向到/ Contact了吗?

/Contact.aspx turned into a 301 redirect to /Contact

If you have a Web Form called /Foo.aspx, you automatically get a /Foo route and can call your page like that! Hence, Microsoft.AspNet.FriendlyUrls.

如果您有一个名为/Foo.aspx的Web窗体,您将自动获得/ Foo路由并可以像这样调用您的页面! 因此,Microsoft.AspNet.FriendlyUrls。

Just by adding the one package and calling

只需添加一个包并调用

routes.EnableFriendlyUrls();

in RouteConfig (this default came down with the NuGet package) my whole WebForms app loses its .ASPX extensions and gets reasonable defaults.

在RouteConfig(默认值随NuGet软件包提供)中,我的整个WebForms应用程序都失去了.ASPX扩展名,并获得了合理的默认值。

FriendlyUrls高级示例 (FriendlyUrls Advanced Sample)

Get it? Ok, let's dig into some of the obvious next questions and some more advanced scenarios. How do I get values out of the URL? I'm used to Request.QueryString and Request.Form, but how do I get ahold of these URL segments?

得到它? 好的,让我们深入探讨一些明显的下一个问题以及一些更高级的方案。 如何从URL中获取值? 我习惯使用Request.QueryString和Request.Form,但是如何获取这些URL段?

Here's a Foo.aspx that I've visited via /Foo.

这是我通过/ Foo访问的Foo.aspx。

A basic Foo WebForms page

If I click "Click Me" the URL points to /Foo/bar/34.

如果单击“单击我”,则URL指向/ Foo / bar / 34

Visiting /Foo/bar/34

NOTE: Be aware of the magic. It makes sense. If there was a 34.aspx in a folder called Bar in a folder called Foo, we would have used that file. There wasn't. If there was a file called Bar.aspx in a folder called Foo we would have used that. There wasn't. So, we used Foo.aspx and passed in the rest of the URL.

注意:注意魔术。 这说得通。 如果在名为Foo的文件夹中名为Bar的文件夹中存在34.aspx,我们将使用该文件。 没有。 如果在名为Foo的文件夹中有一个名为Bar.aspx的文件,我们将使用该文件。 没有。 因此,我们使用了Foo.aspx并传入了其余的URL。

I can get the segments out like this:

我可以像这样取出片段:

<% foreach (var segment in Request.GetFriendlyUrlSegments()) { %>
<li><%: segment %></li>
<% } %>

UPDATE: One thing I forgot to mention was how to get the values out of the FriendlyURL. You can use things like [Form] and [QueryString] to model bind in WebForms. Now you can add [FriendlyUrlSegments] to get data out, like the ID in this example:

更新:我忘记提及的一件事是如何从FriendlyURL中获取值。 您可以使用[Form]和[QueryString]之类的东西来对WebForms中的绑定进行建模。 现在,您可以添加[FriendlyUrlSegments]来获取数据,例如本示例中的ID:

public SomeItem SomeItem_GetItem([FriendlyUrlSegments]int? id)
{
SomeItem item = db.SomeItem.Find(id);
return item;
}

They're sitting on the Request option. I did have to import the Microsoft.AspNet.FriendlyUrls namespace to have this extension appear.

他们坐在“请求”选项上。 我确实必须导入Microsoft.AspNet.FriendlyUrls命名空间才能显示此扩展名。

<%@ Import Namespace="Microsoft.AspNet.FriendlyUrls" %>

Better yet, I can generate Friendly URLs without string concatenation!

更好的是,我可以生成没有字符串连接的友好URL!

<a href="<%: FriendlyUrl.Href("~/Foo", "bar", 34) %>">Click me</a>

Nice, eh? OK, let's make it mobile.

很好,是吗? 好的,让我们使其移动。

使用ASP.NET FriendlyUrl的移动路由 (Mobile Routes with ASP.NET FriendlyUrls)

When you bring down the NuGet package you'll also get a Site.Mobile.Master. If I visit them with the Electric Plum Mobile Simulator (iPhone) I see a default mobile page, automatically.

当您关闭NuGet程序包时,您还将获得一个Site.Mobile.Master。 如果使用Electric Plum Mobile Simulator(iPhone)访问它们,则会自动看到一个默认的移动页面。

The Default Mobile Web Forms page in an iPhone

Ah, you see where this is going. I'll copy Foo.aspx to Foo.Mobile.aspx. I'll make a small change. I'll visit /Foo/bar/34 again except now I get the mobile master and the mobile foo, automatically.

啊,你知道这是怎么回事。 我将Foo.aspx复制到Foo.Mobile.aspx。 我会做一个零钱。 我将再次访问/ Foo / bar / 34,除了现在我会自动获得移动主机和移动foo。

image

What I want to support switching back and forth from Desktop to Mobile? Just add a ViewSwitcher control, also included.

我要支持从台式机到移动设备的来回切换吗? 只需添加一个ViewSwitcher控件(也包括在内)。

<friendlyUrls:ViewSwitcher runat="server" />

Now I re-render and I get a "switch to mobile" and switch to desktop.

现在,我重新渲染,然后得到“切换到移动设备”并切换到桌面。

image

Now I can go back and forth between views and request a desktop site even when on mobile.

现在,即使在移动设备上,我也可以在视图之间来回切换并请求一个桌面站点。

image

So basic mobile is nice but I might want very specific mobile views for iPhone, iPad, Opera Mobile, etc.

因此,基本的移动设备很好,但我可能希望针对iPhone,iPad,Opera Mobile等提供非常具体的移动设备视图。

具有ASP.NET FriendlyUrl的特定设备的超高级移动路由 (Super Advanced Mobile Routes for Specific Devices with ASP.NET FriendlyUrls)

By default FriendlyUrls uses a class called WebFormsFriendlyUrlResolver but you can derive from this class and change its behavior however you like. Here's an example of a "DeviceSpecificWebFormsFriendlyUrlResolver" or, better yet, Mobile Friendly Urls for WebForms.

默认情况下,FriendlyUrls使用一个名为WebFormsFriendlyUrlResolver的类,但是您可以从该类派生并随意更改其行为。 这是“ DeviceSpecificWebFormsFriendlyUrlResolver”的示例,或者更好的是,WebForms的“移动友好的Urls”。

This derived URL resolver does just that, it resolves URLs to physical Web Forms pages. You'd then pass it into the overload of EnableFriendlyUrls(...);

此派生的URL解析器就是这样做的,它将URL解析为物理Web窗体页面。 然后,将其传递给EnableFriendlyUrls(...)的重载;

IMPORTANT NOTE: This code is just a very early sample, there will be a more complete one released later.

重要说明:此代码只是一个非常早期的示例,稍后将发布更完整的示例。

public class DeviceSpecificWebFormsFriendlyUrlResolver : WebFormsFriendlyUrlResolver
{
private readonly IDictionary<string, string> _deviceUserAgentMap = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase)
{
{ "Opera Mobi", "OperaMobile" },
{ "iPhone", "iPhone" },
{ "iPad", "iPad" }
};

protected override IList<string> GetExtensions(HttpContextBase httpContext)
{
var extensions = base.GetExtensions(httpContext).ToList();
if (extensions.Contains(MobileAspxExtension, StringComparer.OrdinalIgnoreCase))
{
// Base has determined we should look for a mobile page, let's add device specific
// extension to the beginning.
var deviceSpecificSufffix = GetDeviceSpecificSuffix(httpContext);
if (!String.IsNullOrEmpty(deviceSpecificSufffix))
{
extensions.Insert(0, "." + deviceSpecificSufffix + AspxExtension);
}
}
return extensions;
}

protected override bool IsMobileExtension(HttpContextBase httpContext, string extension)
{
return base.IsMobileExtension(httpContext, extension) ||
_deviceUserAgentMap.Values.Any(v => extension.Contains(v, StringComparison.OrdinalIgnoreCase));
}

protected override bool TrySetMobileMasterPage(HttpContextBase httpContext, Page page, string mobileSuffix)
{
var deviceSpecificSufffix = GetDeviceSpecificSuffix(httpContext);
if (!String.IsNullOrEmpty(deviceSpecificSufffix) && base.TrySetMobileMasterPage(httpContext, page, deviceSpecificSufffix))
{
// We were able to set a device specific master page, so just return
return true;
}

// Just use the base logic
return base.TrySetMobileMasterPage(httpContext, page, mobileSuffix);
}

private string GetDeviceSpecificSuffix(HttpContextBase httpContext)
{
foreach (var item in _deviceUserAgentMap)
{
if (httpContext.Request.UserAgent.Contains(item.Key, StringComparison.OrdinalIgnoreCase))
{
return item.Value;
}
}

return String.Empty;
}
}

Now we've created a map of device specific suffixes, so we can have not Foo.Mobile.aspx, but rather Foo.iPhone.aspx and Foo.OperaMobile.aspx, etc.

现在,我们已经创建了设备特定后缀的地图,因此我们不能使用Foo.Mobile.aspx,而应该使用Foo.iPhone.aspx和Foo.OperaMobile.aspx等。

Here's a little demo that loads a bunch of names into a list. Here's /async, the desktop view.

这是一个小示例,将一堆名称加载到列表中。 这是/ async桌面视图。

A list of names on the desktop

Now we'll add jQuery mobile to the mobile master page, and use it on the mobile version of the same page. We're still calling the same data source and reusing all that code.

现在,我们将jQuery mobile添加到移动母版页,并在同一页面的移动版上使用它。 我们仍在调用相同的数据源并重用所有这些代码。

The list of names now as a jQuery mobile page inside an iPhone

I'm pretty jazzed about what this means for ASP.NET and Web Forms developers. We're going to continue to push forward and improve ASP.NET even now, after Visual Studio 2012 has been released. Sometimes we'll add small features via NuGet packages, sometimes editor improvements as free VSIX Extensions like the Web Essentials playground for 2012 and larger changes via released updates to all of ASP.NET.  I hope you like the direction we're heading.

我对这对ASP.NET和Web Forms开发人员意味着什么感到非常高兴。 在Visual Studio 2012发布之后,即使现在,我们仍将继续推进和改进ASP.NET。 有时,我们将通过NuGet软件包添加一些小的功能,有时将编辑器作为免费的VSIX扩展进行改进,例如2012年Web Essentials游乐场,并通过对所有ASP.NET的更新发布进行较大的更改。 希望您喜欢我们的前进方向。

Go play with Microsoft.AspNet.FriendlyUrls now and thank Damian and friends on Twitter!

现在就开始使用Microsoft.AspNet.FriendlyUrls并在Twitter上感谢Damian和朋友!

This week's sponsor: Be part of GENERATION APP. Your Idea. Your App. 30 Days. Begin your 30-day journey to create a Windows Store style app and talk 1-on-1 with a Windows 8 app development pro. Get started today.

本周赞助商:成为GENERATION APP的一部分。 你的想法。 您的应用。 30天。 开始为期30天的开发过程,以创建Windows Store风格的应用程序,并与Windows 8应用程序开发人员进行一对一的交谈。 立即开始。

翻译自: https://www.hanselman.com/blog/introducing-aspnet-friendlyurls-cleaner-urls-easier-routing-and-mobile-views-for-aspnet-web-forms

asp.net 关闭父窗体

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值