GVKun编程网logo

asp.net mvc使用html5mode和路由托管角度应用程序(.net mvc 路由)

24

此处将为大家介绍关于asp.netmvc使用html5mode和路由托管角度应用程序的详细内容,并且为您解答有关.netmvc路由的相关问题,此外,我们还将为您介绍关于Angularjshtml5Mo

此处将为大家介绍关于asp.net mvc使用html5mode和路由托管角度应用程序的详细内容,并且为您解答有关.net mvc 路由的相关问题,此外,我们还将为您介绍关于Angularjs html5Mode页面刷新打破了asp.net mvc中的路由、ASP.NET MVC:如何在MVC应用程序中使用静态HTML页面?、asp.net-mvc – asp mvc使用View Model在视图中列出产品详细信息、asp.net-mvc – ASP.NET MVC 2 – Html.DropDownList与ViewModel混淆的有用信息。

本文目录一览:

asp.net mvc使用html5mode和路由托管角度应用程序(.net mvc 路由)

asp.net mvc使用html5mode和路由托管角度应用程序(.net mvc 路由)

好吧,所以我在asp.net mvc 5中托管一个 angularjs,并在我的角应用程序中使用html5mode(true)来摆脱url中的所有哈希标志.
但是,一切都很好,我目前的设置如下所示:

RouteConfig.cs:

routes.MapRoute(
            name: "Default",url: "app/{angular}",defaults: new { controller = "Ng",action = "Index",angular= UrlParameter.Optional }
        );

因此,当我导航到http:// url / app / myangularroute时,我的Ng / Index操作返回包含ng-view容器的视图,并且角度app现在处于活动状态并使用提供的路径

现在,我的问题是,当我导航到http:// url / app /它返回一个dir listning不允许的错误,我无法理解.因为angular参数设置为optional,我的索引操作不应该返回吗?

我可以以某种方式完全避免“应用程序”,仍然让我的角度应用程序工作?我尝试了一些重写规则,但这给了我很多错误,因为我正在使用mvc捆绑和缩小功能.

我可以使用url作为当前格式,但无需提供可选参数,例如http:// url / app /

此外,它只是一个角度应用程序,没有其他mvc视图比index.cshtml包装器.

This guy seems to get it to work,but I can’t see his mvc routes

解决方法

尝试在web.config sytem.webserver设置中添加此项.
<system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>
<system.webServer>

编辑:

尝试更改RouteConfig.cs,如下所示:

routes.MapRoute(
        name: "Default",url: "app/{*.}",action = "Index" }
    );

EDIT2:

我完全忘记了这个问题,但现在我才意识到问题可能是你没有配置你的IIS服务器来使用Html5Mode,看看这个:https://github.com/angular-ui/ui-router/wiki/Frequently-Asked-Questions#how-to-configure-your-server-to-work-with-html5mode

具体来说这部分:

Azure IIS重写:

<system.webServer>
  <rewrite>
    <rules> 
      <rule name="Main Rule" stopProcessing="true">
        <match url=".*" />
        <conditions logicalGrouping="MatchAll">
          <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />                                 
          <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
        </conditions>
        <action type="Rewrite" url="/" />
      </rule>
    </rules>
  </rewrite>
</system.webServer>

我希望这个对你有用.

Angularjs html5Mode页面刷新打破了asp.net mvc中的路由

Angularjs html5Mode页面刷新打破了asp.net mvc中的路由

我在我的asp.net应用程序中执行angularjs路由.我有一个文件夹名称模板,包含3个html页面Home,About,Error.
这是我的app.js文件
var app = angular.module('myApp',['ngRoute','ngAnimate']);

app.controller('HomeController',function ($scope) {
    $scope.Message = "We are in home page";
});
app.controller('AboutController',function ($scope) {
    $scope.Message = "We are in about page";
});
app.controller('ErrorController',function ($scope) {
    $scope.Message = "404 page not found";
});

app.config(['$routeProvider','$locationProvider',function ($routeProvider,$locationProvider) {
    $routeProvider.
         when('/',{
             redirectTo: function () {
                 return '/home';
             }
         }).
        when('/home',{
            templateUrl: '/template/Home.html',controller: 'HomeController'
        }).
        when('/about',{
            templateUrl: '/template/About.html',controller: 'AboutController'
        }).
         when('/error',{
             templateUrl: '/template/Error.html',controller: 'ErrorController'
         }).
    otherwise({
        redirectTo: '/error'
    });
    $locationProvider.html5Mode(true);
}]);

Home.html由

<div ng-controller="HomeController">
<h1>Home Page</h1>
<h3>{{Message}}</h3>

等等.

在我的Layout.cshtml文件中,我包含了模块myApp和base属性.

<!DOCTYPE html>
<html ng-app="myApp">
<head>
    <Meta charset="utf-8" />
    <Meta name="viewport" content="width=device-width" />
    <base href="/" />

正文定义了一些链接

<body>
<div>
    <ul>
        <li><a href="/home">Home</a></li>
        <li><a href="/about">About</a></li>
        <li><a href="/erro">Invalid Url</a></li>
    </ul>
</div>
<div>
    <div data-ng-view="" id="ng-view"></div>
</div>

当我从家里导航到其他人时,路由工作正常.但是当我刷新页面时它会给资源找不到错误.为此我还在我的web.config文件中包含了这样的服务器端重写.

<rewrite>
        <rules> 
          <rule name="Main Rule" stopProcessing="true">
            <match url=".*" />
            <conditions logicalGrouping="MatchAll">
              <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />                                 
              <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
            </conditions>
            <action type="Rewrite" url="/" />
          </rule>
        </rules>
      </rewrite>

问题仍然是需要帮助解决它.谢谢.

我通过在Route.Config文件中进行更改来解决它
routes.MapRoute(
            name: "Default",url: "{controller}/{action}/{id}",defaults: new { controller = "Home",action = "Index",id = UrlParameter.Optional }
        );

用这个代替

routes.MapRoute(
       name: "Application",url: "{*url}",action = "Index" });

ASP.NET MVC:如何在MVC应用程序中使用静态HTML页面?

ASP.NET MVC:如何在MVC应用程序中使用静态HTML页面?

在我正在开发的应用程序中,我想允许用户上传静态HTML页面来替换默认的“用户配置文件”MVC View页面。这可能吗?也就是说,用户上传的HTML页面将完全用完MVC,并且可以包含自己的CSS链接等。

想法?建议?

解决方法

显然,.net MVC框架处理已经存在于图像/ css / js等的静态内容。这只是一个扩展(路由?)的问题,将.html文件直接传递到IIS。再加上一些重写,使漂亮的url应该做的伎俩。

不过,我非常非常警惕,允许用户生成的内容以原始HTML上传的形式,因为您正在离开非常宽敞的门。最好的是,你将会满足人们的垃圾邮件/色情/广告页面。最糟糕的是,您将为人们提供上传跨站点脚本攻击的渠道,并可能上传恶意内容来破坏您的网站。您可以轻松地将现有的表单存储在您的网站上,将一堆垃圾硬编码到其中,并将其从主页中删除,并打破整个事物。

至少您应该解析上传的内容,将其减少到一小段内容,然后将其包装在您自己的内容中。我个人更倾向于向用户提供一个好的所见即所得编辑器来编辑单一内容 – 任何值得它的盐的编辑应该为您提供关于它包含/不包括哪些元素的消毒。然后将此内容片段存储在数据库/光盘上,并通过标准MVC控制器路由请求主页,并加载该内容。

编辑 – 为您请求示例
您应该能够向路由添加一个忽略规则 – 可能已经有这些示例已经破解打开​​了Global.asax文件 – 您将要调用route.IgnoreRoute方法:

routes.IgnoreRoute("UserPages/{*path}");

应该让IIS处理对yourwebsite.com/UserPages/aUser/homepage.html的所有请求 – 您还可以使用通配符片段/约束更漂亮的解决方案

asp.net-mvc – asp mvc使用View Model在视图中列出产品详细信息

asp.net-mvc – asp mvc使用View Model在视图中列出产品详细信息

我想在视图中列出单个产品的详细信息.产品规格会动态变化,因为规格是在表格中逐行添加的,这意味着我们可以为每种产品添加大量规格(如电子商务网站中所做的那样).现在我能够使用ViewBag满足要求,但我决定使用viewmodel作为更好的做法.

型号类:

// Product:
public partial class ProductTable
{
    public ProductTable()
    {
        this.SpecificationsTable = new HashSet<SpecificationsTable>();
    }

    public int ProductID { get; set; }
    public string Title { get; set; }
    public string SmallDescription { get; set; }
    public string FullDescription { get; set; }

    public virtual ICollection<SpecificationsTable> SpecificationsTable { get; set; }
    }

//Specifications:
public partial class SpecificationsTable
{
    public int SpecificationsID { get; set; }
    public string SpecificationName { get; set; }
    public string SpecificationValue { get; set; }
    public Nullable<int> ProductID { get; set; }
    public virtual ProductTable ProductTable { get; set; }
}

视图模型:

public class Detailsviewmodel
{
    public int ProductID { get; set; }
    public string Title { get; set; }
    public string SmallDescription { get; set; }
    public string FullDescription { get; set; }
    public string SpecificationName { get; set; }
    public string SpecificationValue { get; set; }
}

ActionMethod

public ActionResult ProductDetails(int id)
{
    var details = (from c in dbo.ProductTable
                   join s in dbo.SpecificationsTable 
                   on c.ProductID equals s.ProductID
                   where c.ProductID == id
                   select new Detailviewmodel
                   {
                       Title = c.Title,SmallDescription = c.SmallDescription,FullDescription = c.FullDescription
                   }).ToList();

     // To remove repeated product title,small and full description
     var distinctItems = details.GroupBy(x => x.ProductID).Select(y => y.First());

     // To show product title,small and full description for this product

     ViewBag.ProductDetails = distinctItems;

     var specifications = (from c in dbo.ProductTable
                             join s in dbo.SpecificationsTable 
                             on c.ProductID equals s.ProductID
                             where c.ProductID == id
                             select new Detailviewmodel
                             {
                                 SpecificationName = s.SpecificationName,SpecificationValue = s.SpecificationValue
                             }).ToList();

    // To show list of specifications for this product
    ViewBag.Specifcations = specifications;
    return View();
}

预期产量:

细节:

Title: New Samsung offer

SmallDescription : Something small 

FullDescription : Something full

Specifcations:

Mobile Name :Samsung

Model : 2015

Price : 70 $

Color:  White

我正在使用数据库优先方法,我正在尝试学习如何在这里使用视图模型.

解决方法

您当前的视图模型不反映您要在视图中显示的内容,这是每个产品的多个规范,因此您需要一个集合属性.将视图模型更改为

public class SpecificationVM
{
  public string Name { get; set; }
  public string Value { get; set; }
}
public class ProductVM
{
  public string Title { get; set; }
  public string SmallDescription { get; set; }
  public string FullDescription { get; set; }
  IEnumerable<SpecificationVM> Specifications { get; set; }
}

然后在控制器中,使用填充查看模型

public ActionResult ProductDetails(int id)
{
  var product = db.ProductTable.Where(p => p.ProductID == id).FirstOrDefault();
  // note you may need to add .Include("SpecificationsTable") in the above
  if (product == null)
  { 
    return new HttpNotFoundResult();
  }
  ProductVM model = new ProductVM()
  {
    Title = product.Title,SmallDescription = product.SmallDescription,FullDescription = product.FullDescription,Specifications = product.SpecificationsTable.Select(s => new SpecificationVM()
    {
      Name = s.SpecificationName,Value = s.SpecificationValue
    })
  };
  return View(model);
}

然后在视图中

@model yourAssembly.ProductVM
<h2>Details</h2>
@Html.displayNameFor(m => m.Title)
@Html.displayFor(m => m.Title)
.... // ditto for SmallDescription and FullDescription 
<h2>Specifications</h2>
@foreach(var item in Model.Specifications)
{
  @Html.displayFor(m => item.Name)
  @Html.displayFor(m => item.Value)
}

asp.net-mvc – ASP.NET MVC 2 – Html.DropDownList与ViewModel混淆

asp.net-mvc – ASP.NET MVC 2 – Html.DropDownList与ViewModel混淆

我完全迷失了,如何使用新的强类型Html.DropDownListFor帮助ASP.NET MVC 2.0 R2

在视图中我写:

<%= Html.DropDownListFor(m => m.ParentCategory,new SelectList(Model.Categories,"CategoryId","Name",Model.ParentCategory),"[ None ]")%>

<%= Html.ValidationMessageFor(m => m.ParentCategory)%>

因此我的Model对象是:

public class CategoryForm : FormModelBase
{
    public CategoryForm()
    {
        Categories = new List<Category>();

        Categories.Add(new CategoryForm.Category() {
                           CategoryId = 1,Name = "cpus" });
        Categories.Add(new CategoryForm.Category() { 
                           CategoryId = 2,Name = "Memory" });
        Categories.Add(new CategoryForm.Category() { 
                           CategoryId = 3,Name = "Hard drives" });
    }

    // ...other props,snip... //

    public Category ParentCategory { get; set; }

    public IList<Category> Categories { get; protected set; }

    public class Category
    {
        public int? CategoryId { get; set; }
        public string Name { get; set; }
    }
}

问题是,当我从下拉列表中选择一个项目,说第一个项目,我得到以下ValidationMessageFor错误“值’1’无效。

所以我将视图更改为…

<%= Html.DropDownListFor(m => m.ParentCategory.**CategoryId**,new SelectList .../ snip  ) %>

现在它工作,有点。我的viewmodel中的ParentCategory属性设置了正确的“CategoryId”,但“Name”为NULL。我最好只有一个可空的int ParentCategory属性,而不是一个强类型的’类’对象?

解决方法

我也遇到了同样的问题。

当我调试Action并看看ModelState.Values [1] .Errors [0] .Exception例如,我看到以下:

{“The parameter conversion from type ‘System.String’ to type ‘System.Collections.Generic.keyvaluePair`2[[System.String,mscorlib,Version=2.0.0.0,Culture=neutral,PublicKeyToken=b77a5c561934e089],[system.int64,PublicKeyToken=b77a5c561934e089]]’ Failed because no type converter can convert between these types.”} System.Exception {system.invalidOperationException}

在我的场景中,我的SelectList是从字典创建的,我在我的视图中使用:

<%=Html.DropDownListFor(x => x.MyDictionary,new SelectList(
                                 Model.MyDictionary,"Value","Key")) %>

当我把它改为:

<%=Html.DropDownListFor(x => x.MyDictionary.Keys,// <-- changed to .Keys
                             new SelectList(
                                 Model.MyDictionary,"Key")) %>

它工作没有问题。

谢谢。

关于asp.net mvc使用html5mode和路由托管角度应用程序.net mvc 路由的问题我们已经讲解完毕,感谢您的阅读,如果还想了解更多关于Angularjs html5Mode页面刷新打破了asp.net mvc中的路由、ASP.NET MVC:如何在MVC应用程序中使用静态HTML页面?、asp.net-mvc – asp mvc使用View Model在视图中列出产品详细信息、asp.net-mvc – ASP.NET MVC 2 – Html.DropDownList与ViewModel混淆等相关内容,可以在本站寻找。

本文标签: