asp.net-mvc-5

How to generate URL for the action with attribute routing in Asp.Net MVC

北战南征 提交于 2020-07-08 20:37:07
问题 public class HomeController : Controller { [Route("Users/about")] [Route("Users/WhoareWe")] [Route("Users/OurTeam")] [Route("Users/aboutCompany")] public ActionResult GotoAbout() { return View(); } } I have many routes defined for action GotoAbout() . How to create route URL in razor page programmatically when generate URL for action like home/users/about ? 回答1: Reference Attribute Routing in ASP.NET MVC 5 - Route Names You can specify a name for a route, in order to easily allow URI

How to generate URL for the action with attribute routing in Asp.Net MVC

一个人想着一个人 提交于 2020-07-08 20:36:59
问题 public class HomeController : Controller { [Route("Users/about")] [Route("Users/WhoareWe")] [Route("Users/OurTeam")] [Route("Users/aboutCompany")] public ActionResult GotoAbout() { return View(); } } I have many routes defined for action GotoAbout() . How to create route URL in razor page programmatically when generate URL for action like home/users/about ? 回答1: Reference Attribute Routing in ASP.NET MVC 5 - Route Names You can specify a name for a route, in order to easily allow URI

Seed Roles (RoleManager vs RoleStore)

自古美人都是妖i 提交于 2020-06-28 06:46:08
问题 Through looking at the posts here, I've seen two different ways of creating ASP.NET Identity roles through Entity Framework seeding. One way uses RoleManager and the other uses RoleStore . I was wondering if there is a difference between the two. As using the latter will avoid one less initialization string[] roles = { "Admin", "Moderator", "User" }; // Create Role through RoleManager var roleStore = new RoleStore<IdentityRole>(context); var manager = new RoleManager<IdentityRole>(roleStore);

how can i redirect from void method to a view?

…衆ロ難τιáo~ 提交于 2020-06-18 10:55:33
问题 [HttpGet] public void verifyAccount(string id) { if (clientDB.verifyAccount(id)!="") { // Redirect("index");//redirect to index view } else { //redirect to error view } } i try to use Redirect("Index"); but it does not work, Is it impossible to redirect to view from a void method 回答1: You cannot redirect from a method that returns void. In order to redirect, you must return an ActionResult , and since this method returns nothing , you obviously cannot achieve that. Setting the return type of

MVC5 Web API and Dependency Injection

天涯浪子 提交于 2020-06-12 07:15:11
问题 Trying to do some DI on Web API 2 without third-party tools. So, from some examples I've got custom dependency resolver (why there's no integrated one? Strange, even Microsoft.Extensions.DependencyInjection provides nothing): public class DependencyResolver : IDependencyResolver { protected IServiceProvider _serviceProvider; public DependencyResolver(IServiceProvider serviceProvider) { this._serviceProvider = serviceProvider; } public IDependencyScope BeginScope() { return this; } public void

MVC5 Web API and Dependency Injection

倾然丶 夕夏残阳落幕 提交于 2020-06-12 07:14:48
问题 Trying to do some DI on Web API 2 without third-party tools. So, from some examples I've got custom dependency resolver (why there's no integrated one? Strange, even Microsoft.Extensions.DependencyInjection provides nothing): public class DependencyResolver : IDependencyResolver { protected IServiceProvider _serviceProvider; public DependencyResolver(IServiceProvider serviceProvider) { this._serviceProvider = serviceProvider; } public IDependencyScope BeginScope() { return this; } public void

What is the relationship between owin and oAuth2.0?

做~自己de王妃 提交于 2020-06-09 07:40:47
问题 I study external login strategies and the terminology confuses me. What's the relation between the following. Owin OauthWebSecurity OAuth 2.0 Owin Katana ASP.NET Identity 回答1: Owin Owin is no more than a specification. It stands for Open Web Interface for .Net. In very simplistic terms it is based in the idea that using a few language constructs (delegates and a dictionary) you can create a framework for handling web requests that is independent of where it is hosted (you can even run an

How to keep script in partial view from loading more than once and causing errors when partial is used more than once in the same page

牧云@^-^@ 提交于 2020-06-09 05:56:06
问题 Working in ASP.NET MVC, I created a partial view which is rendered on the same page 2 times. My problem is the JavaScript is included as many times as the partial view is and JavaScript does not like classes to be redefined. My question is: How do I include the JavaScript in the partial view so it remains modular, but only have it included in the page once? I am not interested in solutions which require including the .js file separately from the content of the partial view, like <script src=

MVC5 / C# - Cannot perform runtime binding on a null reference

自闭症网瘾萝莉.ら 提交于 2020-05-29 11:03:08
问题 I'm trying to figure out what's causing a Cannot perform runtime binding on a null reference error from this code: var query = "SELECT Id, UserName, List_Order, LoggedIn " + "FROM AspNetUsers" + "WHERE LoggedIn = 1" + "ORDER BY List_Order ASC"; var conn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString); var cmd = new SqlCommand(query, conn); conn.Open(); var rdr = cmd.ExecuteReader(); var n = 0; while(rdr.Read()) { if

how to map composite key in CRUD functionality

|▌冷眼眸甩不掉的悲伤 提交于 2020-05-28 18:53:16
问题 I need to map based on two keys(comp and part). @foreach (var item in Model) { <tr> <td> @Html.DisplayFor(modelItem => item.comp) </td> <td> @Html.DisplayFor(modelItem => item.part) </td> ........................... <td> @Html.ActionLink("Edit", "Edit", new { id=item.comp }) | @Html.ActionLink("Details", "Details", new { id = item.comp }) | @Html.ActionLink("Delete", "Delete", new { id = item.comp }) </td> </tr> } how to use composite key in Index page and also in controller. public