社区
C#
帖子详情
asp.net mvc webapi 动态在controller后面加action
BJ20120308
2014-07-23 02:34:15
比如客户端请求这个链接
http://bbs.csdn.net/open/rest?method=user-info-get
这个时候程序接收到上面的请求后要转换成下面这样的链接执行,controller是rest,user-info-get是action
http://bbs.csdn.net/open/rest/user-info-get
是需要重写routehand吗?
...全文
176
1
打赏
收藏
asp.net mvc webapi 动态在controller后面加action
比如客户端请求这个链接 http://bbs.csdn.net/open/rest?method=user-info-get 这个时候程序接收到上面的请求后要转换成下面这样的链接执行,controller是rest,user-info-get是action http://bbs.csdn.net/open/rest/user-info-get 是需要重写routehand吗?
复制链接
扫一扫
分享
转发到动态
举报
写回复
配置赞助广告
用AI写文章
1 条
回复
切换为时间正序
请发表友善的回复…
发表回复
打赏红包
刘小安
2014-07-23
打赏
举报
回复
action没办法动态的。你这个只能算参数,但是你要调用的必须是存在的。 如果你是想把 http://bbs.csdn.net/open/rest?method=user-info-get后面的参数改为 http://bbs.csdn.net/open/rest/user-info-get ,可以设置路由。动态action是不行的
[
ASP.NET
MVC
小牛之路] 文章系列
asp.net
mvc
系列教程,转载于博客园 一、[
ASP.NET
MVC
小牛之路]01 - 理解
MVC
模式 二、[
ASP.NET
MVC
小牛之路]02 - C#知识点提要 三、[
ASP.NET
MVC
小牛之路]03 - Razor语法 四、[
ASP.NET
MVC
小牛之路]04 - 依赖注入(DI)和Ninject 五、[
ASP.NET
MVC
小牛之路]05 - 使用 Ninject 六、[
ASP.NET
MVC
小牛之路]06 - 使用 Entity Framework 七、[
ASP.NET
MVC
小牛之路]07 - URL Routing 八、[
ASP.NET
MVC
小牛之路]08 - Area 使用 九、[
ASP.NET
MVC
小牛之路]09 -
Controller
和
Action
(1) 十、[
ASP.NET
MVC
小牛之路]10 -
Controller
和
Action
(2) 十一、[
ASP.NET
MVC
小牛之路]11 - Filter 十二、[
ASP.NET
MVC
小牛之路]12 - Section、Partial View 和 Child
Action
十三、[
ASP.NET
MVC
小牛之路]13 - Helper Method 十四、[
ASP.NET
MVC
小牛之路]14 - Unobtrusive Ajax 十五、[
ASP.NET
MVC
小牛之路]15 - Model Binding 十六、[
ASP.NET
MVC
小牛之路]16 - Model 验证 十八、[
ASP.NET
MVC
小牛之路]18 -
Web
API
十九、[
ASP.NET
MVC
大牛之路]01 - 开篇 二十[
ASP.NET
MVC
大牛之路]02 - C#高级知识点概要(1) - 委托和事件 二一、[
ASP.NET
MVC
大牛之路]03 - C#高级知识点概要(2) - 线程和并发
asp.net
mvc
AspNet
MVC
2_RC_VS2008.exe
ASP.NET
MVC
2 Release Candidate Release Notes This document describes the Release Candidate release of the
ASP.NET
MVC
2 framework. Installation Notes 2 Documentation 2 Support 2 Upgrading an
ASP.NET
MVC
1.0 Project to
ASP.NET
MVC
2 2 New Features 3
ASP.NET
MVC
validation scripts have been moved to their own file 4
ASP.NET
MVC
validation scripts can be included at the top or bottom of a page 4
ASP.NET
MVC
validation scripts support globalization 4 Html.ValidationSummary helper method can display model-level errors 4 T4 templates in Visual Studio generate code that is specific to the target version of the .NET Framework 4 Other Improvements 4 Bug Fixes 5 Breaking Changes 5 Changes in
ASP.NET
MVC
2 Release Candidate 5 Changes in
ASP.NET
MVC
2 Beta 6 Changes in
ASP.NET
MVC
2 Preview 2 6 Changes in
ASP.NET
MVC
2 Preview 1 7 Known Issues 7 Disclaimer 8 This document describes the Release Candidate release of
ASP.NET
MVC
2 for Visual Studio 2008 SP1. Installation Notes The
ASP.NET
MVC
2 Release Candidate for Visual Studio 2008 SP1 can be downloaded from the following page: http://go.microsoft.com/fwlink/?LinkID=157071
ASP.NET
MVC
2 can be installed and can run side-by-side with
ASP.NET
MVC
1.0. Note Because Visual Studio 2008 and Visual Studio 2010 Beta 2 share a component of
ASP.NET
MVC
2, installing the
ASP.NET
MVC
2 Release Candidate release on a computer where Visual Studio 2010 Beta 2 is also installed is not supported. Documentation Documentation for
ASP.NET
MVC
2, which includes tutorials, technology overviews, code samples, and
API
reference, is available on the MSDN
Web
site. A good starting point is the
ASP.NET
Model View
Controller
(
MVC
) topic (http://go.microsoft.com/fwlink/?LinkId=159758). Tutorials and other information about
ASP.NET
MVC
are also available on the
ASP.NET
Web
site (http://www.
asp.net
/
mvc
/). Support This is a Release Candidate (RC) release and is not officially supported. If you have questions about working with this release, post them to the
ASP.NET
MVC
forum (http://forums.
asp.net
/1146.aspx), where members of the
ASP.NET
community are frequently able to provide informal support. Upgrading an
ASP.NET
MVC
1.0 Project to
ASP.NET
MVC
2 To upgrade an existing
ASP.NET
MVC
1.0 application to version 2, follow these steps: Make a backup of the existing project. Open the project file in a text editor (the file with the .csproj or .vbproj file extension) and find the ProjectTypeGuid element. As the value of that element, replace the GUID {603c0e0b-db56-11dc-be95-000d561079b0} with {F85E285D-A4E0-4152-9332-AB1D724D3325}. When you are done, the value of that element should be as follows:
{F85E285D-A4E0-4152-9332-AB1D724D3325};{349c5851-65df-11da-9384-00065b846f21};{fae04ec0-301f-11d3-bf4b-00c04f79efbc}
In the
Web
application root folder, edit the
Web
.config file. Search for System.
Web
.
Mvc
, Version=1.0.0.0 and replace all instances with System.
Web
.
Mvc
, Version=2.0.0.0. Repeat the previous step for the
Web
.config file located in the Views directory. Open the project using Visual Studio, and in Solution Explorer, expand the References node. Delete the reference to System.
Web
.
Mvc
(which points to the version 1.0 assembly). Add a reference to System.
Web
.
Mvc
(v2.0.0.0). Add the following bindingRedirect element to the
Web
.config file in the application root under the configuraton section:
Create a new
ASP.NET
MVC
2 application. Copy the files from the Scripts directory of the new application into the Scripts directory of the existing application. Compile the application and run it. If any errors occur, refer to the Breaking Changes section of this document for possible solutions. New Features This section describes features that have been introduced in the
ASP.NET
MVC
2 Release Candidate release.
ASP.NET
MVC
validation scripts have been moved to their own file To help reduce conflicts with other Ajax libraries, the built-in
ASP.NET
MVC
validation scripts are now in a separate JavaScript file.
ASP.NET
MVC
validation scripts can be included at the top or bottom of a page The server component that renders the client validation metadata now renders the metadata into a JavaScript variable. This allows the validation scripts to be included either at the top of the page or at the bottom, because the scripts will look for the variable and load the metadata when it is available.
ASP.NET
MVC
validation scripts support globalization When the required
ASP.NET
Ajax globalization scripts are included in a page, the validation logic uses the culture-specific data for data types (such as dates and numbers) when it validates input text. The new Html.GlobalizationScript helper method can be used to render a reference to the globalization script for the current culture. Html.ValidationSummary helper method can display model-level errors Instead of always displaying all validation errors, the Html.ValidationSummary helper method has a new option to display only model-level errors. This enables model-level errors to be displayed in the validation summary and field-specific errors next to each field. T4 templates in Visual Studio generate code that is specific to the target version of the .NET Framework A new property is available to T4 files from the
ASP.NET
MVC
T4 host that specifies the version of the .NET Framework that is used by the application. This allows T4 templates to generate code and markup that is specific to a version of the .NET Framework. In Visual Studio 2008, the value is always .NET 3.5. In Visual Studio 2010, the value is either .NET 3.5 or .NET 4. Other Improvements The following additional changes have been made to existing types and members for the
ASP.NET
MVC
2 Release Candidate release. The default HTML markup that is generated by the Add View dialog box has been changed to be consistent with the markup that is rendered by the templated helpers (Editor, EditorFor, Display, and DisplayFor). Buttons in a form can specify that they do not cause validation logic to run. The default is that every button in a form causes validation logic to run, and if validation fails, the validation logic blocks submission of the form. Enabling validation selectively for buttons lets you create forms that can post information even if a form is not complete or if the form contains data that is temporarily invalid. Client validators can specify when they run. Possible values are input (while the user is typing), blur (after the user is done typing and moves to another field), and submit (when the user submits the form). Bug Fixes The following bugs have been fixed in the
ASP.NET
MVC
2 Release Candidate release. The FileResult
action
result now supports non-US-ASCII characters in file names. Methods and properties of the TempDataDictionary class have been improved to better handle the case when items are flagged for removal from the dictionary. Support for validation in the IDataErrorInfo interface has been fixed. Breaking Changes The following changes might cause errors in existing
ASP.NET
MVC
1.0 applications. Changes in
ASP.NET
MVC
2 Release Candidate IIS script mapping script is no longer available in the installer The IIS script mapping script is a command-line script that is used to configure script maps for IIS 6 and for IIS 7 in Classic mode. The script-mapping script is not needed if you use the Visual Studio Development Server or if you use IIS 7 in Integrated mode. The scripts are available as a separate unsupported download on the
ASP.NET
CodePlex site. The Html.Substitute helper method in
MVC
Futures is no longer available Due to changes in the rendering behavior of
MVC
view engines, the Html.Substitute helper method does not work and has been removed. Changes in
ASP.NET
MVC
2 Beta The IValueProvider interface replaces all uses of IDictionary
Every property or method argument that accepted IDictionary
now accepts IValueProvider. This change affects only applications that include custom value providers or custom model binders. Examples of properties and methods that are affected by this change include the following: The ValueProvider property of the
Controller
Base and ModelBindingContext classes. The TryUpdateModel methods of the
Controller
class. New CSS classes were added in the Site.css file that are used to style validation messages. Changes in
ASP.NET
MVC
2 Preview 2 Helpers now return an
Mvc
HtmlString object In order to take advantage of the new HTML-encoding expression syntax in
ASP.NET
4, the return type for HTML helpers is now
Mvc
HtmlString instead of a string. Note that if you use
ASP.NET
MVC
2 and the new helpers with
ASP.NET
3.5, you will not be able to take advantage of the HTML-encoding syntax; the new syntax is available only when you run
ASP.NET
MVC
2 on
ASP.NET
4. JsonResult now responds only to HTTP POST requests In order to mitigate JSON hijacking attacks that have the potential for information disclosure, by default, the JsonResult class now responds only to HTTP POST requests. Ajax GET calls to
action
methods that return a JsonResult object should be changed to use POST instead. If necessary, you can override this behavior by setting the new JsonRequestBehavior property of JsonResult. For more information about the potential exploit, see the blog post JSON Hijacking on Phil Haack’s blog. Model and ModelType property setters on ModelBindingContext are obsolete A new settable ModelMetadata property has been added to the ModelBindingContext class. The new property encapsulates both the Model and the ModelType properties. Although the Model and ModelType properties are obsolete, for backward compatibility the property getters still work; they delegate to the ModelMetadata property to retrieve the value. Changes in
ASP.NET
MVC
2 Preview 1 Changes to the Default
Controller
Factory class break custom
controller
factories that derive from it The Default
Controller
Factory class was fixed by removing the RequestContext property. In place of this property, the request context instance is passed to the protected virtual Get
Controller
Instance and Get
Controller
Type methods. This change affects custom
controller
factories that derive from Default
Controller
Factory. Custom
controller
factories are often used to provide dependency injection for
ASP.NET
MVC
applications. To update the custom
controller
factories to support
ASP.NET
MVC
2, change the method signature or signatures to match the new signatures, and use the request context parameter instead of the property. “Area” is a now a reserved route-value key The string “area” in Route values now has special meaning in
ASP.NET
MVC
, in the same way that “
controller
” and “
action
” do. One implication is that if HTML helpers are supplied with a route-value dictionary containing “area”, the helpers will no longer append “area” in the query string. If you are using the Areas feature, make sure to not use {area} as part of your route URL. Known Issues Adding a debugger watch that shows a value from TempData will mark it for deletion. The side effect was introduced as part of the changes to TempDataDictionary. Disclaimer This is a preliminary document and may be changed substantially prior to final commercial release of the software described herein. The information contained in this document represents the current view of Microsoft Corporation on the issues discussed as of the date of publication. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information presented after the date of publication. This White Paper is for informational purposes only. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS DOCUMENT. Complying with all applicable copyright laws is the responsibility of the user. Without limiting the rights under copyright, no part of this document may be reproduced, stored in or introduced into a retrieval system, or transmitted in any form or by any means (electronic, mechanical, photocopying, recording, or otherwise), or for any purpose, without the express written permission of Microsoft Corporation. Microsoft may have patents, patent applications, trademarks, copyrights, or other intellectual property rights covering subject matter in this document. Except as expressly provided in any written license agreement from Microsoft, the furnishing of this document does not give you any license to these patents, trademarks, copyrights, or other intellectual property. Unless otherwise noted, the example companies, organizations, products, domain names, e-mail addresses, logos, people, places and events depicted herein are fictitious, and no association with any real company, organization, product, domain name, email address, logo, person, place or event is intended or should be inferred. © 2009 Microsoft Corporation. All rights reserved. Microsoft and Windows are either registered trademarks or trademarks of Microsoft Corporation in the United States and/or other countries. The names of actual companies and products mentioned herein may be the trademarks of their respective owners.
ASP.NET
中
Web
API
的简单实例
一、
Web
API
的路由 1、在Visual Studio中新建
MVC
4项目,在App_Start目录下有一个
Web
Api
Config.cs文件,这个文件中就是相应的
Web
API
的路由配置了。 2、
Web
API
框架默认是基于 Restful 架构模式的,与
ASP.NET
MVC
有区别的是,它会根据 Http 请求的 HttpMethod(Get、Post、Put、Delete)来在
Controller
中查找
Action
,规则是:
Action
名中是否以Get、Post 开头?
Action
上标记 HttpGet、HttpPost 等标记? 3、当然可以修改默认的配置,让客户端在调用
为
ASP.NET
MVC
及
Web
Api
添
加
路由优先级
一、为什么需要路由优先级 大家都知道我们在
Asp.Net
MVC
项目或
Web
Api
项目中注册路由是没有优先级的,当项目比较大、或有多个区域、或多个
Web
项目、或采用插件式框架开发时,我们的路由注册很可能 不是写在一个文件中的,而是分散在很多不同项目的文件中,这样一来,路由的优先级的问题就突显出来了。 比如: App_Start/RouteConfig.cs中 routes.MapRoute( name: Default, url: {
controller
}/{
action
}/{id}, defaults: new {
controller
= Home,
action
ASP.NET
Core中的
Action
的返回值类型实现
在
Asp.net
Core之前所有的
Action
返回值都是
Action
Result,Json(),File()等方法返回的都是
Action
Result的子类。并且Core把
MVC
跟
Web
Api
合并之后
Action
的返回值体系也有了很大的变化。
Action
Result类
Action
Result类是最常用的返回值类型。基本沿用了之前
Asp.net
MVC
的那套东西,使用它大部分情况都没问题。比如用它来返回视图,返回json,返回文件等等。如果是异步则使用Task。 public class Test
Controller
:
Controller
{ publi
C#
110,561
社区成员
642,566
社区内容
发帖
与我相关
我的任务
C#
.NET技术 C#
复制链接
扫一扫
分享
社区描述
.NET技术 C#
社区管理员
加入社区
获取链接或二维码
近7日
近30日
至今
加载中
查看更多榜单
社区公告
让您成为最强悍的C#开发者
试试用AI创作助手写篇文章吧
+ 用AI写文章